@@ -8,5 +8,68 @@ public static byte[] SubArray(byte[] bytes, int index, int length)
8
8
{
9
9
return new List < byte > ( bytes ) . GetRange ( index , length ) . ToArray ( ) ;
10
10
}
11
+
12
+ public static byte [ ] getInitialBytesFromEncodedType ( byte [ ] encodedType )
13
+ {
14
+ byte [ ] initialBytes = { Protocol . ROOM_DATA } ;
15
+
16
+ if ( encodedType . Length < 0x20 )
17
+ {
18
+ initialBytes = addByteToArray ( initialBytes , new byte [ ] { ( byte ) ( encodedType . Length | 0xa0 ) } ) ;
19
+ }
20
+ else if ( encodedType . Length < 0x100 )
21
+ {
22
+ initialBytes = addByteToArray ( initialBytes , new byte [ ] { 0xd9 } ) ;
23
+ initialBytes = uint8 ( initialBytes , encodedType . Length ) ;
24
+ }
25
+ else if ( encodedType . Length < 0x10000 )
26
+ {
27
+ initialBytes = addByteToArray ( initialBytes , new byte [ ] { 0xda } ) ;
28
+ initialBytes = uint16 ( initialBytes , encodedType . Length ) ;
29
+ }
30
+ else if ( encodedType . Length < 0x7fffffff )
31
+ {
32
+ initialBytes = addByteToArray ( initialBytes , new byte [ ] { 0xdb } ) ;
33
+ initialBytes = uint32 ( initialBytes , encodedType . Length ) ;
34
+ }
35
+ else
36
+ {
37
+ throw new System . Exception ( "String too long" ) ;
38
+ }
39
+
40
+ return initialBytes ;
41
+ }
42
+
43
+ private static byte [ ] addByteToArray ( byte [ ] byteArray , byte [ ] newBytes )
44
+ {
45
+ byte [ ] bytes = new byte [ byteArray . Length + newBytes . Length ] ;
46
+ System . Buffer . BlockCopy ( byteArray , 0 , bytes , 0 , byteArray . Length ) ;
47
+ System . Buffer . BlockCopy ( newBytes , 0 , bytes , byteArray . Length , newBytes . Length ) ;
48
+ return bytes ;
49
+ }
50
+
51
+ private static byte [ ] uint8 ( byte [ ] bytes , int value )
52
+ {
53
+ return addByteToArray ( bytes , new byte [ ] { ( byte ) ( value & 255 ) } ) ;
54
+ }
55
+
56
+ private static byte [ ] uint16 ( byte [ ] bytes , int value )
57
+ {
58
+ var a1 = addByteToArray ( bytes , new byte [ ] { ( byte ) ( value & 255 ) } ) ;
59
+ return addByteToArray ( a1 , new byte [ ] { ( byte ) ( ( value >> 8 ) & 255 ) } ) ;
60
+ }
61
+
62
+ private static byte [ ] uint32 ( byte [ ] bytes , int value )
63
+ {
64
+ var b4 = value >> 24 ;
65
+ var b3 = value >> 16 ;
66
+ var b2 = value >> 8 ;
67
+ var b1 = value ;
68
+ var a1 = addByteToArray ( bytes , new byte [ ] { ( byte ) ( b1 & 255 ) } ) ;
69
+ var a2 = addByteToArray ( a1 , new byte [ ] { ( byte ) ( b2 & 255 ) } ) ;
70
+ var a3 = addByteToArray ( a2 , new byte [ ] { ( byte ) ( b3 & 255 ) } ) ;
71
+ return addByteToArray ( a3 , new byte [ ] { ( byte ) ( b4 & 255 ) } ) ;
72
+ }
73
+ }
11
74
}
12
75
}
0 commit comments