annotate libpurple/protocols/gg/lib/deflate.c @ 32074:896e1e0dbe35

Forgot two files for the gg 1.11.0 update
author Ethan Blanton <elb@pidgin.im>
date Sun, 05 Jun 2011 14:15:04 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32074
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
1 /* $Id$ */
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
2
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
3 /*
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
4 * (C) Copyright 2011 Bartosz Brachaczek <b.brachaczek@gmail.com>
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
5 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
7 * it under the terms of the GNU Lesser General Public License Version
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
8 * 2.1 as published by the Free Software Foundation.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
9 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
13 * GNU Lesser General Public License for more details.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
14 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
16 * License along with this program; if not, write to the Free Software
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
18 * USA.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
19 */
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
20
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
21 /**
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
22 * \file deflate.c
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
23 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
24 * \brief Funkcje kompresji Deflate
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
25 */
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
26
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
27 #include <stdlib.h>
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
28 #include <string.h>
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
29
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
30 #include "libgadu.h"
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
31 #include "deflate.h"
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
32
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
33 #ifdef GG_CONFIG_HAVE_ZLIB
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
34 #include <zlib.h>
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
35 #endif
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
36
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
37 /**
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
38 * \internal Kompresuje dane wejściowe algorytmem Deflate z najwyższym
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
39 * stopniem kompresji, tak samo jak oryginalny klient.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
40 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
41 * Wynik funkcji należy zwolnić za pomocą \c free.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
42 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
43 * \param in Ciąg znaków do skompresowania, zakończony \c \\0
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
44 * \param out_lenp Wskaźnik na zmienną, do której zostanie zapisana
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
45 * długość bufora wynikowego
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
46 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
47 * \return Skompresowany ciąg znaków lub \c NULL w przypadku niepowodzenia.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
48 */
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
49 unsigned char *gg_deflate(const char *in, size_t *out_lenp)
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
50 {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
51 #ifdef GG_CONFIG_HAVE_ZLIB
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
52 int ret;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
53 z_stream strm;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
54 unsigned char *out, *out2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
55 size_t out_len;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
56
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
57 if (in == NULL || out_lenp == NULL)
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
58 return NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
59
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
60 strm.zalloc = Z_NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
61 strm.zfree = Z_NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
62 strm.opaque = Z_NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
63 strm.avail_in = strlen(in);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
64 strm.next_in = (unsigned char*) in;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
65
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
66 ret = deflateInit(&strm, Z_BEST_COMPRESSION);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
67 if (ret != Z_OK) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
68 gg_debug(GG_DEBUG_MISC, "// gg_deflate() deflateInit() failed (%d)\n", ret);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
69 return NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
70 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
71
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
72 out_len = deflateBound(&strm, strm.avail_in);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
73 out = malloc(out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
74
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
75 if (out == NULL) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
76 gg_debug(GG_DEBUG_MISC, "// gg_deflate() not enough memory for output data (%d)\n", out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
77 goto fail;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
78 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
79
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
80 strm.avail_out = out_len;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
81 strm.next_out = out;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
82
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
83 for (;;) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
84 ret = deflate(&strm, Z_FINISH);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
85
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
86 if (ret == Z_STREAM_END)
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
87 break;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
88
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
89 /* raczej nie powinno się zdarzyć przy Z_FINISH i out_len == deflateBound(),
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
90 * ale dokumentacja zlib nie wyklucza takiej możliwości */
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
91 if (ret == Z_OK) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
92 out_len *= 2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
93 out2 = realloc(out, out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
94
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
95 if (out2 == NULL) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
96 gg_debug(GG_DEBUG_MISC, "// gg_deflate() not enough memory for output data (%d)\n", out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
97 goto fail;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
98 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
99
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
100 out = out2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
101
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
102 strm.avail_out = out_len / 2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
103 strm.next_out = out + out_len / 2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
104 } else {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
105 gg_debug(GG_DEBUG_MISC, "// gg_deflate() deflate() failed (ret=%d, msg=%s)\n", ret, strm.msg != NULL ? strm.msg : "no error message provided");
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
106 goto fail;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
107 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
108 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
109
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
110 out_len = strm.total_out;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
111 out2 = realloc(out, out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
112
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
113 if (out2 == NULL) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
114 gg_debug(GG_DEBUG_MISC, "// gg_deflate() not enough memory for output data (%d)\n", out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
115 goto fail;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
116 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
117
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
118 *out_lenp = out_len;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
119 deflateEnd(&strm);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
120
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
121 return out2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
122
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
123 fail:
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
124 *out_lenp = 0;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
125 deflateEnd(&strm);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
126 free(out);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
127 #endif
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
128 return NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
129 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
130
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
131 /**
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
132 * \internal Dekompresuje dane wejściowe w formacie Deflate.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
133 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
134 * Wynik funkcji należy zwolnić za pomocą \c free.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
135 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
136 * \param in Bufor danych skompresowanych algorytmem Deflate
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
137 * \param length Długość bufora wejściowego
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
138 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
139 * \note Dokleja \c \\0 na końcu bufora wynikowego.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
140 *
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
141 * \return Zdekompresowany ciąg znaków, zakończony \c \\0,
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
142 * lub \c NULL w przypadku niepowodzenia.
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
143 */
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
144 char *gg_inflate(const unsigned char *in, size_t length)
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
145 {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
146 #ifdef GG_CONFIG_HAVE_ZLIB
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
147 int ret;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
148 z_stream strm;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
149 char *out = NULL, *out2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
150 size_t out_len = 1024;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
151 int first = 1;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
152
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
153 if (in == NULL)
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
154 return NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
155
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
156 strm.zalloc = Z_NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
157 strm.zfree = Z_NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
158 strm.opaque = Z_NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
159 strm.avail_in = length;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
160 strm.next_in = (unsigned char*) in;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
161
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
162 ret = inflateInit(&strm);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
163 if (ret != Z_OK) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
164 gg_debug(GG_DEBUG_MISC, "// gg_inflate() inflateInit() failed (%d)\n", ret);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
165 return NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
166 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
167
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
168 do {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
169 out_len *= 2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
170 out2 = realloc(out, out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
171
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
172 if (out2 == NULL) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
173 gg_debug(GG_DEBUG_MISC, "// gg_inflate() not enough memory for output data (%d)\n", out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
174 goto fail;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
175 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
176
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
177 out = out2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
178
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
179 if (first) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
180 strm.avail_out = out_len;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
181 strm.next_out = (unsigned char*) out;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
182 } else {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
183 strm.avail_out = out_len / 2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
184 strm.next_out = (unsigned char*) out + out_len / 2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
185 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
186
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
187 ret = inflate(&strm, Z_NO_FLUSH);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
188
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
189 if (ret != Z_OK && ret != Z_STREAM_END) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
190 gg_debug(GG_DEBUG_MISC, "// gg_inflate() inflate() failed (ret=%d, msg=%s)\n", ret, strm.msg != NULL ? strm.msg : "no error message provided");
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
191 goto fail;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
192 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
193
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
194 first = 0;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
195 } while (ret != Z_STREAM_END);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
196
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
197 /* rezerwujemy ostatni znak na NULL-a */
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
198 out_len = strm.total_out + 1;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
199 out2 = realloc(out, out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
200
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
201 if (out2 == NULL) {
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
202 gg_debug(GG_DEBUG_MISC, "// gg_inflate() not enough memory for output data (%d)\n", out_len);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
203 goto fail;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
204 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
205
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
206 out = out2;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
207 out[out_len - 1] = '\0';
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
208
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
209 inflateEnd(&strm);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
210
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
211 return out;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
212
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
213 fail:
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
214 inflateEnd(&strm);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
215 free(out);
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
216 #endif
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
217 return NULL;
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
218 }
896e1e0dbe35 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
219