aboutsummaryrefslogtreecommitdiffstats
path: root/facebook/facebook-util.c
blob: 5cea8075ff0fbff85e2b0c0c04f4d1c62f28ae3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright 2014 James Geboski <jgeboski@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdarg.h>
#include <string.h>
#include <zlib.h>

#include "facebook-util.h"

/**
 * Determines the debugging state of the plugin.
 *
 * @return TRUE if debugging is enabled, otherwise FALSE.
 **/
#ifdef DEBUG_FACEBOOK
gboolean fb_util_debugging(void)
{
    static gboolean debug = FALSE;
    static gboolean setup = FALSE;

    if (G_UNLIKELY(!setup)) {
        debug = g_getenv("BITLBEE_DEBUG") ||
                g_getenv("BITLBEE_DEBUG_FACEBOOK");
        setup = TRUE;
    }

    return debug;
}
#endif /* DEBUG_FACEBOOK */

/**
 * Dumps a #GByteArray to the debugging stream. This formats the output
 * similar to that of `hexdump -C`.
 *
 * @param bytes  The #GByteArray.
 * @param indent The indent width.
 * @param fmt    The format string or NULL.
 * @param ...    The format arguments.
 **/
#ifdef DEBUG_FACEBOOK
void fb_util_hexdump(const GByteArray *bytes, guint indent,
                     const gchar *fmt, ...)
{
    GString *gstr;
    va_list  ap;
    gchar   *instr;
    guint    i;
    guint    j;
    gchar    c;

    if (fmt != NULL) {
        va_start(ap, fmt);
        instr = g_strdup_vprintf(fmt, ap);
        FB_UTIL_DEBUGLN("%s", instr);
        g_free(instr);
        va_end(ap);
    }

    instr = g_strnfill(indent, ' ');
    gstr  = g_string_sized_new(80);
    i     = 0;

    if (G_UNLIKELY(bytes == NULL))
        goto finish;

    for (; i < bytes->len; i += 16) {
        g_string_append_printf(gstr, "%s%08x  ", instr, i);

        for (j = 0; j < 16; j++) {
            if ((i + j) < bytes->len) {
                g_string_append_printf(gstr, "%02x ", bytes->data[i + j]);
            } else {
                g_string_append(gstr, "   ");
            }

            if (j == 7)
                g_string_append_c(gstr, ' ');
        }

        g_string_append(gstr, " |");

        for (j = 0; (j < 16) && ((i + j) < bytes->len); j++) {
            c = bytes->data[i + j];

            if (!g_ascii_isprint(c) || g_ascii_isspace(c))
                c = '.';

            g_string_append_c(gstr, c);
        }

        g_string_append_c(gstr, '|');
        FB_UTIL_DEBUGLN("%s", gstr->str);
        g_string_erase(gstr, 0, -1);
    }

finish:
    g_string_append_printf(gstr, "%s%08x", instr, i);
    FB_UTIL_DEBUGLN("%s", gstr->str);

    g_string_free(gstr, TRUE);
    g_free(instr);
}
#endif /* DEBUG_FACEBOOK */

/**
 * Compare two strings case insensitively. This is useful for where
 * the return value must be a boolean, such as with a #GEqualFunc.
 *
 * @param s1 The first string.
 * @param s2 The second string.
 *
 * @return TRUE if the strings are equal, otherwise FALSE.
 **/
gboolean fb_util_str_iequal(const gchar *s1, const gchar *s2)
{
    return g_ascii_strcasecmp(s1, s2) == 0;
}

/**
 * Implemented #alloc_func for #g_malloc().
 *
 * @param opaque The user-defined data, which is NULL.
 * @param items  The number of items.
 * @param size   The size of each item.
 *
 * @return The pointer to the allocated memory.
 **/
static voidpf fb_util_zalloc(voidpf opaque, uInt items, uInt size)
{
    return g_malloc(size * items);
}

/**
 * Implemented #free_func for #g_free().
 *
 * @param opaque  The user-defined data, which is NULL.
 * @param address The pointer address.
 **/
static void fb_util_zfree(voidpf opaque, voidpf address)
{
    g_free(address);
}

/**
 * Determines if a #GByteArray is zlib compressed.
 *
 * @param bytes The #GByteArray.
 *
 * @return TRUE if the #GByteArray is compressed, otherwise FALSE.
 **/
gboolean fb_util_zcompressed(const GByteArray *bytes)
{
    guint8 b0;
    guint8 b1;

    g_return_if_fail(bytes != NULL);

    if (bytes->len < 2)
        return FALSE;

    b0 = *(bytes->data + 0);
    b1 = *(bytes->data + 1);

    return ((((b0 << 8) | b1) % 31) == 0) && /* Check the header */
           ((b0 & 0x0F) == Z_DEFLATED);      /* Check the method */
}

/**
 * Compresses a #GByteArray with zlib. The returned #GByteArray should
 * be freed with #g_byte_array_free() when no longer needed.
 *
 * @param bytes The #GByteArray.
 *
 * @return The resulting #GByteArray, or NULL on error.
 **/
GByteArray *fb_util_zcompress(const GByteArray *bytes)
{
    GByteArray *ret;
    z_stream    zs;
    gsize       size;
    gint        res;

    g_return_if_fail(bytes != 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);
    return ret;
}

/**
 * Uncompresses a zlib compressed #GByteArray. The returned #GByteArray
 * should be freed with #g_byte_array_free() when no longer needed.
 *
 * @param bytes The #GByteArray.
 *
 * @return The resulting #GByteArray, or NULL on error.
 **/
GByteArray *fb_util_zuncompress(const GByteArray *bytes)
{
    GByteArray *ret;
    z_stream    zs;
    guint8      out[1024];
    gint        res;

    g_return_if_fail(bytes != 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);
    return ret;
}