1 /**************************************************************************/
2 /*                                                                        */
3 /* Copyright (c) 2001, 2010 NoMachine, http://www.nomachine.com/.         */
4 /*                                                                        */
5 /* NXCOMP, NX protocol compression and NX extensions to this software     */
6 /* are copyright of NoMachine. Redistribution and use of the present      */
7 /* software is allowed according to terms specified in the file LICENSE   */
8 /* which comes in the source distribution.                                */
9 /*                                                                        */
10 /* Check http://www.nomachine.com/licensing.html for applicability.       */
11 /*                                                                        */
12 /* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
13 /*                                                                        */
14 /* All rights reserved.                                                   */
15 /*                                                                        */
16 /**************************************************************************/
17 
18 #include "ChangeGCCompat.h"
19 
20 #include "ClientCache.h"
21 
22 #include "EncodeBuffer.h"
23 #include "DecodeBuffer.h"
24 
25 //
26 // Set the verbosity level.
27 //
28 
29 #define PANIC
30 #define WARNING
31 #undef  TEST
32 #undef  DEBUG
33 #undef  DUMP
34 
35 //
36 // Here are the methods to handle messages' content.
37 //
38 
parseIdentity(Message * message,const unsigned char * buffer,unsigned int size,int bigEndian) const39 int ChangeGCCompatStore::parseIdentity(Message *message, const unsigned char *buffer,
40                                            unsigned int size, int bigEndian) const
41 {
42   ChangeGCCompatMessage *changeGC = (ChangeGCCompatMessage *) message;
43 
44   //
45   // Here is the fingerprint.
46   //
47 
48   changeGC -> gcontext   = GetULONG(buffer + 4, bigEndian);
49   changeGC -> value_mask = GetULONG(buffer + 8, bigEndian);
50 
51   //
52   // Clear the unused bytes carried in the
53   // payload to increase the effectiveness
54   // of the caching algorithm.
55   //
56 
57   if ((int) size > dataOffset)
58   {
59     #ifdef DEBUG
60     *logofs << name() << ": Removing unused bytes from the "
61             << "data payload.\n" << logofs_flush;
62     #endif
63 
64     changeGC -> value_mask &= (1 << 23) - 1;
65 
66     unsigned int mask = 0x1;
67     unsigned char *source = (unsigned char *) buffer + CHANGEGC_DATA_OFFSET;
68     unsigned long value = 0;
69 
70     for (unsigned int i = 0; i < 23; i++)
71     {
72       if (changeGC -> value_mask & mask)
73       {
74         value = GetULONG(source, bigEndian);
75 
76         value &= (0xffffffff >> (32 - CREATEGC_FIELD_WIDTH[i]));
77 
78         PutULONG(value, source, bigEndian);
79 
80         source += 4;
81       }
82 
83       mask <<= 1;
84     }
85   }
86 
87   #ifdef DEBUG
88   *logofs << name() << ": Parsed Identity for message at "
89           << this << ".\n" << logofs_flush;
90   #endif
91 
92   return 1;
93 }
94 
unparseIdentity(const Message * message,unsigned char * buffer,unsigned int size,int bigEndian) const95 int ChangeGCCompatStore::unparseIdentity(const Message *message, unsigned char *buffer,
96                                              unsigned int size, int bigEndian) const
97 {
98   ChangeGCCompatMessage *changeGC = (ChangeGCCompatMessage *) message;
99 
100   //
101   // Fill all the message's fields.
102   //
103 
104   PutULONG(changeGC -> gcontext,   buffer + 4, bigEndian);
105   PutULONG(changeGC -> value_mask, buffer + 8, bigEndian);
106 
107   #ifdef DEBUG
108   *logofs << name() << ": Unparsed identity for message at "
109           << this << ".\n" << logofs_flush;
110   #endif
111 
112   return 1;
113 }
114 
dumpIdentity(const Message * message) const115 void ChangeGCCompatStore::dumpIdentity(const Message *message) const
116 {
117   #ifdef DUMP
118 
119   ChangeGCCompatMessage *changeGC = (ChangeGCCompatMessage *) message;
120 
121   *logofs << name() << ": Identity gcontext " << changeGC -> gcontext
122                     << ", mask " << changeGC -> value_mask << ", size "
123                     << changeGC -> size_ << ".\n" << logofs_flush;
124   #endif
125 }
126 
identityChecksum(const Message * message,const unsigned char * buffer,unsigned int size,int bigEndian) const127 void ChangeGCCompatStore::identityChecksum(const Message *message, const unsigned char *buffer,
128                                                unsigned int size, int bigEndian) const
129 {
130   md5_append(md5_state_, buffer + 4, 8);
131 }
132