libpurple/protocols/gg/lib/deflate.c

Wed, 28 Sep 2016 09:32:19 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Wed, 28 Sep 2016 09:32:19 -0500
changeset 38066
2e94b6fa06a0
parent 35627
fd11790cc4d6
permissions
-rw-r--r--

Merged in CMaiku/pidgin (pull request #136)

Windows build fixes

31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
1 /* $Id$ */
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
2
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
3 /*
906428efd520 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>
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
5 *
906428efd520 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
906428efd520 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
906428efd520 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.
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
9 *
906428efd520 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,
906428efd520 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
906428efd520 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
906428efd520 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.
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
14 *
906428efd520 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
906428efd520 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
906428efd520 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,
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
18 * USA.
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
19 */
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
20
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
21 /**
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
22 * \file deflate.c
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
23 *
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
24 * \brief Funkcje kompresji Deflate
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
25 */
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
26
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
27 #include <stdlib.h>
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
28 #include <string.h>
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
29
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
30 #include "libgadu.h"
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
31 #include "internal.h"
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
32 #include "deflate.h"
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
33
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
34 #ifdef GG_CONFIG_HAVE_ZLIB
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
35 #include <zlib.h>
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
36 #endif
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
37
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
38 /**
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
39 * \internal Kompresuje dane wejściowe algorytmem Deflate z najwyższym
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
40 * stopniem kompresji, tak samo jak oryginalny klient.
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
41 *
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
42 * Wynik funkcji należy zwolnić za pomocą \c free.
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
43 *
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
44 * \param in Ciąg znaków do skompresowania, zakończony \c \\0
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
45 * \param out_lenp Wskaźnik na zmienną, do której zostanie zapisana
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
46 * długość bufora wynikowego
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
47 *
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
48 * \return Skompresowany ciąg znaków lub \c NULL w przypadku niepowodzenia.
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
49 */
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
50 unsigned char *gg_deflate(const char *in, size_t *out_lenp)
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
51 {
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
52 #ifdef GG_CONFIG_HAVE_ZLIB
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
53 int ret;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
54 z_stream strm;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
55 unsigned char *out, *out2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
56 size_t out_len;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
57
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
58 if (in == NULL || out_lenp == NULL)
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
59 return NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
60
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
61 strm.zalloc = Z_NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
62 strm.zfree = Z_NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
63 strm.opaque = Z_NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
64 strm.avail_in = strlen(in);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
65 strm.next_in = (unsigned char*) in;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
66
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
67 ret = deflateInit(&strm, Z_BEST_COMPRESSION);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
68 if (ret != Z_OK) {
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
69 gg_debug(GG_DEBUG_MISC, "// gg_deflate() deflateInit() failed (%d)\n", ret);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
70 return NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
71 }
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
72
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
73 out_len = deflateBound(&strm, strm.avail_in);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
74 out = malloc(out_len);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
75
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
76 if (out == NULL) {
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
77 gg_debug(GG_DEBUG_MISC, "// gg_deflate() not enough memory for "
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
78 "output data (%" GG_SIZE_FMT ")\n", out_len);
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
79 goto fail;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
80 }
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
81
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
82 strm.avail_out = out_len;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
83 strm.next_out = out;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
84
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
85 for (;;) {
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
86 ret = deflate(&strm, Z_FINISH);
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
87
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
88 if (ret == Z_STREAM_END)
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
89 break;
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
90
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
91 /* raczej nie powinno się zdarzyć przy Z_FINISH i out_len == deflateBound(),
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
92 * ale dokumentacja zlib nie wyklucza takiej możliwości */
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
93 if (ret == Z_OK) {
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
94 out_len *= 2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
95 out2 = realloc(out, out_len);
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
96
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
97 if (out2 == NULL) {
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
98 gg_debug(GG_DEBUG_MISC, "// gg_deflate() not "
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
99 "enough memory for output data (%"
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
100 GG_SIZE_FMT ")\n", out_len);
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
101 goto fail;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
102 }
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
103
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
104 out = out2;
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
105
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
106 strm.avail_out = out_len / 2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
107 strm.next_out = out + out_len / 2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
108 } else {
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
109 gg_debug(GG_DEBUG_MISC, "// gg_deflate() deflate() "
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
110 "failed (ret=%d, msg=%s)\n", ret,
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
111 strm.msg != NULL ? strm.msg :
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
112 "no error message provided");
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
113 goto fail;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
114 }
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
115 }
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
116
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
117 out_len = strm.total_out;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
118 out2 = realloc(out, out_len);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
119
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
120 if (out2 == NULL) {
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
121 gg_debug(GG_DEBUG_MISC, "// gg_deflate() not enough memory for "
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
122 "output data (%" GG_SIZE_FMT ")\n", out_len);
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
123 goto fail;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
124 }
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
125
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
126 *out_lenp = out_len;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
127 deflateEnd(&strm);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
128
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
129 return out2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
130
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
131 fail:
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
132 *out_lenp = 0;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
133 deflateEnd(&strm);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
134 free(out);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
135 #endif
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
136 return NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
137 }
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
138
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
139 /**
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
140 * \internal Dekompresuje dane wejściowe w formacie Deflate.
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
141 *
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
142 * Wynik funkcji należy zwolnić za pomocą \c free.
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
143 *
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
144 * \param in Bufor danych skompresowanych algorytmem Deflate
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
145 * \param length Długość bufora wejściowego
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
146 *
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
147 * \note Dokleja \c \\0 na końcu bufora wynikowego.
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
148 *
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
149 * \return Zdekompresowany ciąg znaków, zakończony \c \\0,
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
150 * lub \c NULL w przypadku niepowodzenia.
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
151 */
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
152 char *gg_inflate(const unsigned char *in, size_t length)
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
153 {
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
154 #ifdef GG_CONFIG_HAVE_ZLIB
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
155 int ret;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
156 z_stream strm;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
157 char *out = NULL, *out2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
158 size_t out_len = 1024;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
159 int first = 1;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
160
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
161 if (in == NULL)
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
162 return NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
163
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
164 strm.zalloc = Z_NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
165 strm.zfree = Z_NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
166 strm.opaque = Z_NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
167 strm.avail_in = length;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
168 strm.next_in = (unsigned char*) in;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
169
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
170 ret = inflateInit(&strm);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
171 if (ret != Z_OK) {
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
172 gg_debug(GG_DEBUG_MISC, "// gg_inflate() inflateInit() failed (%d)\n", ret);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
173 return NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
174 }
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
175
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
176 do {
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
177 out_len *= 2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
178 out2 = realloc(out, out_len);
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
179
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
180 if (out2 == NULL) {
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
181 gg_debug(GG_DEBUG_MISC, "// gg_inflate() not enough "
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
182 "memory for output data (%" GG_SIZE_FMT ")\n", out_len);
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
183 goto fail;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
184 }
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
185
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
186 out = out2;
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
187
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
188 if (first) {
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
189 strm.avail_out = out_len;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
190 strm.next_out = (unsigned char*) out;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
191 } else {
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
192 strm.avail_out = out_len / 2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
193 strm.next_out = (unsigned char*) out + out_len / 2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
194 }
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
195
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
196 ret = inflate(&strm, Z_NO_FLUSH);
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
197
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
198 if (ret != Z_OK && ret != Z_STREAM_END) {
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
199 gg_debug(GG_DEBUG_MISC, "// gg_inflate() inflate() "
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
200 "failed (ret=%d, msg=%s)\n", ret,
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
201 strm.msg != NULL ? strm.msg :
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
202 "no error message provided");
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
203 goto fail;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
204 }
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
205
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
206 first = 0;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
207 } while (ret != Z_STREAM_END);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
208
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
209 /* rezerwujemy ostatni znak na NULL-a */
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
210 out_len = strm.total_out + 1;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
211 out2 = realloc(out, out_len);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
212
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
213 if (out2 == NULL) {
35627
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
214 gg_debug(GG_DEBUG_MISC, "// gg_inflate() not enough memory for "
fd11790cc4d6 Update internal libgadu to the current version from git
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 31828
diff changeset
215 "output data (%" GG_SIZE_FMT ")\n", out_len);
31828
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
216 goto fail;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
217 }
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
218
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
219 out = out2;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
220 out[out_len - 1] = '\0';
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
221
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
222 inflateEnd(&strm);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
223
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
224 return out;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
225
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
226 fail:
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
227 inflateEnd(&strm);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
228 free(out);
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
229 #endif
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
230 return NULL;
906428efd520 Forgot two files for the gg 1.11.0 update
Ethan Blanton <elb@pidgin.im>
parents:
diff changeset
231 }

mercurial