aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/programs/Xserver/hw/nxagent/compext/Z.c
blob: 594269dbed0b932ef4f4b6eed63b8a375c3d32e6 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com)          */
/* Copyright (c) 2008-2014 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de>  */
/* Copyright (c) 2014-2016 Ulrich Sibiller <uli42@gmx.de>                 */
/* Copyright (c) 2014-2016 Mihai Moldovan <ionic@ionic.de>                */
/* Copyright (c) 2011-2016 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>*/
/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com)           */
/*                                                                        */
/* NXCOMPEXT, NX protocol compression and NX extensions to this software  */
/* are copyright of the aforementioned persons and companies.             */
/*                                                                        */
/* Redistribution and use of the present software is allowed according    */
/* to terms specified in the file LICENSE which comes in the source       */
/* distribution.                                                          */
/*                                                                        */
/* All rights reserved.                                                   */
/*                                                                        */
/* NOTE: This software has received contributions from various other      */
/* contributors, only the core maintainers and supporters are listed as   */
/* copyright holders. Please contact us, if you feel you should be listed */
/* as copyright holder, as well.                                          */
/*                                                                        */
/**************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <zlib.h>

#include "Compext.h"

#include "Z.h"

#define PANIC
#define WARNING
#undef  TEST
#undef  DEBUG

#define Z_COMPRESSION_LEVEL      4
#define Z_COMPRESSION_THRESHOLD  32
#define Z_COMPRESSION_STRATEGY   Z_DEFAULT_STRATEGY

static int zCompressionLevel    = Z_COMPRESSION_LEVEL;
static int zCompressionStrategy = Z_COMPRESSION_STRATEGY;

static z_stream *zStream;

static int zInitialized;

static int ZConfigure(int level, int strategy);

static int ZDeflate(char *dest, unsigned int *destLen,
                        const char *source, unsigned int sourceLen);

char *ZCompressData(const char *plainData, unsigned int plainSize, int threshold,
                        int level, int strategy, unsigned int *compressedSize)
{
  char *compressedData;

  /*
   * Determine the size of the source image
   * data and make sure there is enough
   * space in the destination buffer.
   */

  *compressedSize = plainSize + (plainSize / 1000) + 12 + 1;

  compressedData = malloc(*compressedSize);

  if (compressedData == NULL)
  {
    #ifdef PANIC
    fprintf(stderr, "******ZCompressData: PANIC! Failed to allocate [%d] bytes for the destination.\n",
                *compressedSize);
    #endif

    *compressedSize = 0;

    return NULL;
  }

  if (level == Z_NO_COMPRESSION || plainSize < threshold)
  {
    #ifdef TEST
    fprintf(stderr, "******ZCompressData: Not compressing [%d] bytes with level [%d] and "
	    "threshold [%d].\n", plainSize, level, threshold);
    #endif

    /*
     * Tell in the first byte of the buffer
     * if the remaining data is compressed
     * or not. This same byte can be used
     * in future to store some other flag.
     */

    *compressedData = 0;

    memcpy(compressedData + 1, plainData, plainSize);

    *compressedSize = plainSize + 1;

    return compressedData;
  }
  else
  {
    int result;

    /*
     * Reconfigure the stream if needed.
     */

    if (zCompressionLevel != level ||
            zCompressionStrategy != strategy)
    {
      ZConfigure(level, strategy);

      zCompressionLevel    = level;
      zCompressionStrategy = strategy;
    }

    result = ZDeflate(compressedData + 1, compressedSize, plainData, plainSize);

    if (result != Z_OK)
    {
      #ifdef PANIC
      fprintf(stderr, "******ZCompressData: PANIC! Failed to compress [%d] bytes with error [%s].\n",
                  plainSize, zError(result));
      #endif

      free(compressedData);

      *compressedSize = 0;

      return NULL;
    }

    #ifdef TEST
    fprintf(stderr, "******ZCompressData: Source data of [%d] bytes compressed to [%d].\n",
                plainSize, *compressedSize);
    #endif

    *compressedData = 1;

    *compressedSize = *compressedSize + 1;

    return compressedData;
  }
}

int ZConfigure(int level, int strategy)
{
  /*
   * ZLIB wants the avail_out to be
   * non zero, even if the stream was
   * already flushed.
   */

  unsigned char dest[1];

  zStream -> next_out  = dest;
  zStream -> avail_out = 1;

  if (deflateParams(zStream, level, strategy) != Z_OK)
  {
    #ifdef PANIC
    fprintf(stderr, "******ZConfigure: PANIC! Failed to set level to [%d] and strategy to [%d].\n",
                level, strategy);
    #endif

    return -1;
  }
  #ifdef TEST
  else
  {
    fprintf(stderr, "******ZConfigure: Reconfigured the stream with level [%d] and strategy [%d].\n",
                level, strategy);
  }
  #endif

  return 1;
}

int ZDeflate(char *dest, unsigned int *destLen, const char *source, unsigned int sourceLen)
{
  int saveOut;
  int result;

  /*
   * Deal with the possible overflow.
   */

  if (zStream -> total_out & 0x80000000)
  {
    #ifdef TEST
    fprintf(stderr, "******ZDeflate: Reset Z stream counters with total in [%ld] total out [%ld].\n",
                zStream -> total_in, zStream -> total_out);
    #endif

    zStream -> total_in  = 0;
    zStream -> total_out = 0;
  }

  saveOut = zStream -> total_out;

  zStream -> next_in  = (Bytef *) source;
  zStream -> avail_in = (uInt) sourceLen;

  #ifdef MAXSEG_64K

  /*
   * Check if the source is greater
   * than 64K on a 16-bit machine.
   */

  if ((uLong) zStream -> avail_in != sourceLen) return Z_BUF_ERROR;

  #endif

  zStream -> next_out  = (unsigned char *) dest;
  zStream -> avail_out = (uInt) *destLen;

  if ((uLong) zStream -> avail_out != *destLen) return Z_BUF_ERROR;

  result = deflate(zStream, Z_FINISH);

  if (result != Z_STREAM_END)
  {
    deflateReset(zStream);

    return (result == Z_OK ? Z_BUF_ERROR : result);
  }

  *destLen = zStream -> total_out - saveOut;

  result = deflateReset(zStream);

  return result;
}

int ZInitEncoder(void)
{
  if (zInitialized == 0)
  {
    int result;

    zStream = malloc(sizeof(z_stream));

    if (zStream == NULL)
    {
      #ifdef PANIC
      fprintf(stderr, "******ZInitEncoder: PANIC! Failed to allocate memory for the stream.\n");
      #endif

      return -1;
    }

    zStream -> zalloc = (alloc_func) 0;
    zStream -> zfree  = (free_func) 0;
    zStream -> opaque = (voidpf) 0;

    #ifdef TEST
    fprintf(stderr, "******ZInitEncoder: Initializing compressor with level [%d] and startegy [%d].\n",
                zCompressionLevel, zCompressionStrategy);
    #endif

    result = deflateInit2(zStream, zCompressionLevel, Z_DEFLATED,
                              15, 9, zCompressionStrategy);

    if (result != Z_OK)
    {
      #ifdef PANIC
      fprintf(stderr, "******ZInitEncoder: Failed to initialize the compressor with error [%s].\n",
                  zError(result));
      #endif

      return -1;
    }

    zInitialized = 1;
  }

  return zInitialized;
}

int ZResetEncoder(void)
{
  int result;

  if (zInitialized == 1)
  {
    result = deflateEnd(zStream);

    if (result != Z_OK)
    {
      #ifdef WARNING
      fprintf(stderr, "******ZResetEncoder: WARNING! Failed to deinitialize the compressor with error [%s].\n",
                  zError(result));
      #endif
    }

    free(zStream);
  }

  zInitialized = 0;

  return 1;
}