SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CheckSum.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Oculus VR, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
21 #include "slikenet/CheckSum.h"
22 
23 /****************************************************************************
24 * CheckSum::add
25 * Inputs:
26 * unsigned int d: word to add
27 * Result: void
28 *
29 * Effect:
30 * Adds the bytes of the unsigned int to the CheckSum
31 ****************************************************************************/
32 
33 void CheckSum::Add ( unsigned int value )
34 {
35  union
36  {
37  unsigned int value;
38  unsigned char bytes[ 4 ];
39  }
40 
41  data;
42  data.value = value;
43 
44  for ( unsigned int i = 0; i < sizeof( data.bytes ); i++ )
45  Add ( data.bytes[ i ] )
46 
47  ;
48 } // CheckSum::add(unsigned int)
49 
50 /****************************************************************************
51 * CheckSum::add
52 * Inputs:
53 * unsigned short value:
54 * Result: void
55 *
56 * Effect:
57 * Adds the bytes of the unsigned short value to the CheckSum
58 ****************************************************************************/
59 
60 void CheckSum::Add ( unsigned short value )
61 {
62  union
63  {
64  unsigned short value;
65  unsigned char bytes[ 2 ];
66  }
67 
68  data;
69  data.value = value;
70 
71  for ( unsigned int i = 0; i < sizeof( data.bytes ); i++ )
72  Add ( data.bytes[ i ] )
73 
74  ;
75 } // CheckSum::add(unsigned short)
76 
77 /****************************************************************************
78 * CheckSum::add
79 * Inputs:
80 * unsigned char value:
81 * Result: void
82 *
83 * Effect:
84 * Adds the byte to the CheckSum
85 ****************************************************************************/
86 
87 void CheckSum::Add ( unsigned char value )
88 {
89  unsigned char cipher = (unsigned char)( value ^ ( r >> 8 ) );
90  r = ( cipher + r ) * c1 + c2;
91  sum += cipher;
92 } // CheckSum::add(unsigned char)
93 
94 
95 /****************************************************************************
96 * CheckSum::add
97 * Inputs:
98 * LPunsigned char b: pointer to byte array
99 * unsigned int length: count
100 * Result: void
101 *
102 * Effect:
103 * Adds the bytes to the CheckSum
104 ****************************************************************************/
105 
106 void CheckSum::Add ( unsigned char *b, unsigned int length )
107 {
108  for ( unsigned int i = 0; i < length; i++ )
109  Add ( b[ i ] )
110 
111  ;
112 } // CheckSum::add(LPunsigned char, unsigned int)