aboutsummaryrefslogtreecommitdiff
path: root/nxcomp/src/PutImage.cpp
blob: 0422fc8cde0b0ed8c2bd485bc5f09f2a80870e1d (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**************************************************************************/
/*                                                                        */
/* 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)           */
/*                                                                        */
/* NXCOMP, 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.nxcomp 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.                                          */
/*                                                                        */
/**************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "PutImage.h"

#include "ClientCache.h"

#include "EncodeBuffer.h"
#include "DecodeBuffer.h"

#include "WriteBuffer.h"

//
// Set the verbosity level.
//

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

//
// Constructors and destructors.
//

PutImageStore::PutImageStore(StaticCompressor *compressor)

  : MessageStore(compressor)
{
  enableCache    = PUTIMAGE_ENABLE_CACHE;
  enableData     = PUTIMAGE_ENABLE_DATA;

  // Since ProtoStep7 (#issue 108)
  enableCompress = PUTIMAGE_ENABLE_COMPRESS_IF_PROTO_STEP_7;

  dataLimit  = PUTIMAGE_DATA_LIMIT;
  dataOffset = PUTIMAGE_DATA_OFFSET;

  cacheSlots          = PUTIMAGE_CACHE_SLOTS;
  cacheThreshold      = PUTIMAGE_CACHE_THRESHOLD;
  cacheLowerThreshold = PUTIMAGE_CACHE_LOWER_THRESHOLD;

  // Since ProtoStep8 (#issue 108)
  enableSplit = PUTIMAGE_ENABLE_SPLIT_IF_PROTO_STEP_8;

  messages_ -> resize(cacheSlots);

  for (T_messages::iterator i = messages_ -> begin();
           i < messages_ -> end(); i++)
  {
    *i = NULL;
  }

  temporary_ = NULL;
}

PutImageStore::~PutImageStore()
{
  for (T_messages::iterator i = messages_ -> begin();
           i < messages_ -> end(); i++)
  {
    destroy(*i);
  }

  destroy(temporary_);
}

//
// Here are the methods to handle messages' content.
//

int PutImageStore::encodeIdentity(EncodeBuffer &encodeBuffer, const unsigned char *buffer,
                                      const unsigned int size, int bigEndian,
                                          ChannelCache *channelCache) const
{
  ClientCache *clientCache = (ClientCache *) channelCache;

  #ifdef DEBUG
  *logofs << name() << ": Encoding full message identity.\n" << logofs_flush;
  #endif

  encodeBuffer.encodeValue(GetUINT(buffer + 2, bigEndian), 16, 8);

  encodeBuffer.encodeValue((unsigned int) buffer[1], 2);

  encodeBuffer.encodeXidValue(GetULONG(buffer + 4, bigEndian),
                     clientCache -> drawableCache);
  encodeBuffer.encodeXidValue(GetULONG(buffer + 8, bigEndian),
                     clientCache -> gcCache);

  unsigned int width = GetUINT(buffer + 12, bigEndian);
  encodeBuffer.encodeCachedValue(width, 16,
                     clientCache -> putImageWidthCache, 8);

  unsigned int height = GetUINT(buffer + 14, bigEndian);
  encodeBuffer.encodeCachedValue(height, 16,
                     clientCache -> putImageHeightCache, 8);

  unsigned int x = GetUINT(buffer + 16, bigEndian);
  int xDiff = x - clientCache -> putImageLastX;
  clientCache -> putImageLastX = x;
  encodeBuffer.encodeCachedValue(xDiff, 16,
                     clientCache -> putImageXCache, 8);

  unsigned int y = GetUINT(buffer + 18, bigEndian);
  int yDiff = y - clientCache -> putImageLastY;
  clientCache -> putImageLastY = y;
  encodeBuffer.encodeCachedValue(yDiff, 16,
                     clientCache -> putImageYCache, 8);

  encodeBuffer.encodeCachedValue(buffer[20], 8,
                     clientCache -> putImageLeftPadCache);

  encodeBuffer.encodeCachedValue(buffer[21], 8,
                     clientCache -> depthCache);

  #ifdef DEBUG
  *logofs << name() << ": Encoded full message identity.\n" << logofs_flush;
  #endif

  return 1;
}

int PutImageStore::decodeIdentity(DecodeBuffer &decodeBuffer, unsigned char *&buffer,
                                      unsigned int &size, int bigEndian, WriteBuffer *writeBuffer,
                                          ChannelCache *channelCache) const
{
  ClientCache *clientCache = (ClientCache *) channelCache;

  #ifdef DEBUG
  *logofs << name() << ": Decoding full message identity.\n" << logofs_flush;
  #endif

  unsigned int  value;
  unsigned char cValue;

  decodeBuffer.decodeValue(value, 16, 8);

  size = (value << 2);

  buffer = writeBuffer -> addMessage(size);

  decodeBuffer.decodeValue(value, 2);
  buffer[1] = (unsigned char) value;

  decodeBuffer.decodeXidValue(value, clientCache -> drawableCache);
  PutULONG(value, buffer + 4, bigEndian);

  decodeBuffer.decodeXidValue(value, clientCache -> gcCache);
  PutULONG(value, buffer + 8, bigEndian);

  unsigned int width;
  decodeBuffer.decodeCachedValue(width, 16,
                     clientCache -> putImageWidthCache, 8);
  PutUINT(width, buffer + 12, bigEndian);

  unsigned int height;
  decodeBuffer.decodeCachedValue(height, 16,
                     clientCache -> putImageHeightCache, 8);
  PutUINT(height, buffer + 14, bigEndian);

  decodeBuffer.decodeCachedValue(value, 16,
                     clientCache -> putImageXCache, 8);
  clientCache -> putImageLastX += value;
  clientCache -> putImageLastX &= 0xffff;
  PutUINT(clientCache -> putImageLastX, buffer + 16, bigEndian);

  decodeBuffer.decodeCachedValue(value, 16,
                     clientCache -> putImageYCache, 8);
  clientCache -> putImageLastY += value;
  clientCache -> putImageLastY &= 0xffff;
  PutUINT(clientCache -> putImageLastY, buffer + 18, bigEndian);

  decodeBuffer.decodeCachedValue(cValue, 8,
                     clientCache -> putImageLeftPadCache);
  buffer[20] = cValue;

  decodeBuffer.decodeCachedValue(cValue, 8,
                     clientCache -> depthCache);
  buffer[21] = cValue;

  #ifdef DEBUG
  *logofs << name() << ": Decoded full message identity.\n" << logofs_flush;
  #endif

  return 1;
}

int PutImageStore::parseIdentity(Message *message, const unsigned char *buffer,
                                     unsigned int size, int bigEndian) const
{
  PutImageMessage *putImage = (PutImageMessage *) message;

  //
  // Here is the fingerprint.
  //

  putImage -> format   = *(buffer + 1);
  putImage -> depth    = *(buffer + 21);
  putImage -> left_pad = *(buffer + 20);

  putImage -> width  = GetUINT(buffer + 12, bigEndian);
  putImage -> height = GetUINT(buffer + 14, bigEndian);
  putImage -> pos_x  = GetUINT(buffer + 16, bigEndian);
  putImage -> pos_y  = GetUINT(buffer + 18, bigEndian);

  putImage -> drawable = GetULONG(buffer + 4, bigEndian);
  putImage -> gcontext = GetULONG(buffer + 8, bigEndian);

  #ifdef DEBUG
  *logofs << name() << ": Parsed identity for message at " << message << ".\n" << logofs_flush;
  #endif

  return 1;
}

int PutImageStore::unparseIdentity(const Message *message, unsigned char *buffer,
                                       unsigned int size, int bigEndian) const
{
  PutImageMessage *putImage = (PutImageMessage *) message;

  //
  // Fill all the message's fields.
  //

  *(buffer + 1) = putImage -> format;

  PutULONG(putImage -> drawable, buffer + 4, bigEndian);
  PutULONG(putImage -> gcontext, buffer + 8, bigEndian);

  PutUINT(putImage -> width, buffer + 12, bigEndian);
  PutUINT(putImage -> height, buffer + 14, bigEndian);
  PutUINT(putImage -> pos_x, buffer + 16, bigEndian);
  PutUINT(putImage -> pos_y, buffer + 18, bigEndian);

  *(buffer + 20) = (unsigned char) putImage -> left_pad;
  *(buffer + 21) = (unsigned char) putImage -> depth;

  #ifdef DEBUG
  *logofs << name() << ": Unparsed identity for message at "
          << message << ".\n" << logofs_flush;
  #endif

  return 1;
}

void PutImageStore::dumpIdentity(const Message *message) const
{
  #ifdef DUMP

  PutImageMessage *putImage = (PutImageMessage *) message;

  *logofs << name() << ": Identity format " << (unsigned) putImage -> format
          << ", depth " << (unsigned) putImage -> depth << ", left_pad "
          << (unsigned) putImage -> left_pad << ", width " << putImage -> width
          << ", height " << putImage -> height << ", pos_x " << putImage -> pos_x
          << ", pos_y " << putImage -> pos_y << ", drawable " << putImage -> drawable
          << ", gcontext " << putImage -> gcontext  << ", size " << putImage -> size_
          << ".\n" << logofs_flush;

  #endif
}

void PutImageStore::identityChecksum(const Message *message, const unsigned char *buffer,
                                         unsigned int size, int bigEndian) const
{
  //
  // Fields format, width, height, left_pad, depth.
  //

  md5_append(md5_state_, buffer + 1,  1);
  md5_append(md5_state_, buffer + 12, 4);
  md5_append(md5_state_, buffer + 20, 2);
}

void PutImageStore::updateIdentity(EncodeBuffer &encodeBuffer, const Message *message,
                                       const Message *cachedMessage,
                                           ChannelCache *channelCache) const
{
  //
  // Encode the variant part.
  //

  PutImageMessage *putImage       = (PutImageMessage *) message;
  PutImageMessage *cachedPutImage = (PutImageMessage *) cachedMessage;

  ClientCache *clientCache = (ClientCache *) channelCache;

  #ifdef TEST
  *logofs << name() << ": Encoding value " << putImage -> drawable
          << " as drawable field.\n" << logofs_flush;
  #endif

  encodeBuffer.encodeXidValue(putImage -> drawable, clientCache -> drawableCache);

  cachedPutImage -> drawable = putImage -> drawable;

  #ifdef TEST
  *logofs << name() << ": Encoding value " << putImage -> gcontext
          << " as gcontext field.\n" << logofs_flush;
  #endif

  encodeBuffer.encodeXidValue(putImage -> gcontext, clientCache -> gcCache);

  cachedPutImage -> gcontext = putImage -> gcontext;

  #ifdef TEST
  *logofs << name() << ": Encoding value " << putImage -> pos_x
          << " as " << "pos_x" << " field.\n" << logofs_flush;
  #endif

  unsigned short int diff_x = putImage -> pos_x - cachedPutImage -> pos_x;

  encodeBuffer.encodeCachedValue(diff_x, 16,
                     clientCache -> putImageXCache, 8);

  cachedPutImage -> pos_x = putImage -> pos_x;

  #ifdef TEST
  *logofs << name() << ": Encoding value " << putImage -> pos_y
          << " as " << "pos_y" << " field.\n" << logofs_flush;
  #endif

  unsigned short int diff_y = putImage -> pos_y - cachedPutImage -> pos_y;

  encodeBuffer.encodeCachedValue(diff_y, 16,
                     clientCache -> putImageYCache, 8);

  cachedPutImage -> pos_y = putImage -> pos_y;
}

void PutImageStore::updateIdentity(DecodeBuffer &decodeBuffer, const Message *message,
                                       ChannelCache *channelCache) const
{
  //
  // Decode the variant part.
  //

  PutImageMessage *putImage = (PutImageMessage *) message;

  ClientCache *clientCache = (ClientCache *) channelCache;

  unsigned int value;

  decodeBuffer.decodeXidValue(value, clientCache -> drawableCache);

  putImage -> drawable = value;

  #ifdef DEBUG
  *logofs << name() << ": Decoded value " << putImage -> drawable
          << " as drawable field.\n" << logofs_flush;
  #endif

  decodeBuffer.decodeXidValue(value, clientCache -> gcCache);

  putImage -> gcontext = value;

  #ifdef DEBUG
  *logofs << name() << ": Decoded value " << putImage -> gcontext
          << " as gcontext field.\n" << logofs_flush;
  #endif

  decodeBuffer.decodeCachedValue(value, 16,
               clientCache -> putImageXCache, 8);

  putImage -> pos_x += value;
  putImage -> pos_x &= 0xffff;

  #ifdef DEBUG
  *logofs << name() << ": Decoded value " << putImage -> pos_x
          << " as pos_x field.\n" << logofs_flush;
  #endif

  decodeBuffer.decodeCachedValue(value, 16,
               clientCache -> putImageYCache, 8);

  putImage -> pos_y += value;
  putImage -> pos_y &= 0xffff;

  #ifdef DEBUG
  *logofs << name() << ": Decoded value " << putImage -> pos_y
          << " as pos_y field.\n" << logofs_flush;
  #endif
}