Debian Codegraph @

protolib/src/common/protoPkt.cpp

raw
1 /**
2 * @file protoPkt.cpp
3 *
4 * @brief This is a base class that maintains a 32-bit aligned buffer for "packet" (or message) building and parsing
5 */
6 #include "protoDebug.h"
7 #include "protoPkt.h"
8
9 ProtoPkt::ProtoPkt(void* bufferPtr, unsigned int numBytes, bool freeOnDestruct)
10 : buffer_ptr((UINT32*)bufferPtr), buffer_allocated(freeOnDestruct ? (UINT32*)bufferPtr : NULL),
11 buffer_bytes(numBytes), pkt_length(0)
12 {
13
14 }
15
16 ProtoPkt::~ProtoPkt()
17 {
18 if (NULL != buffer_allocated)
19 {
20 buffer_ptr = NULL;
21 delete[] buffer_allocated;
22 buffer_allocated = NULL;
23 buffer_bytes = 0;
24 }
25 }
26
27 UINT8
28 ProtoPkt::GetUINT8Bits(unsigned int byteOffset, unsigned int bitOffset, unsigned int bitLength) const
29 {
30 if(bitOffset>7)
31 {
32 byteOffset += bitOffset/8;
33 bitOffset = bitOffset % 8;
34 }
35 UINT8 ans = 0;
36 if(bitOffset==0)
37 {
38 ans = ((UINT8*)buffer_ptr)[byteOffset];
39 } else {
40 UINT8 byte1 = ((UINT8*)buffer_ptr)[byteOffset];
41 UINT8 byte2 = ((UINT8*)buffer_ptr)[byteOffset+1];
42 ans = (byte1 << bitOffset) | (byte2>>(8-bitOffset));
43 }
44 ans = ans >> (8-bitLength);
45 return ans;
46 }
47
48 void
49 ProtoPkt::SetUINT8Bits(unsigned int byteOffset,unsigned int bitOffset,UINT8 value, unsigned int bitLength)
50 {
51 if(bitOffset>7)
52 {
53 byteOffset += bitOffset/8;
54 bitOffset = bitOffset % 8;
55 }
56 value = value << (8-bitLength); //left align the bits
57 if(bitOffset==0)
58 {
59 UINT8 mask = 0xff >> (bitLength);
60 ((UINT8*)buffer_ptr)[byteOffset] = (((UINT8*)buffer_ptr)[byteOffset] & mask) | value;
61 }
62 else
63 {
64 UINT8 byte1 = ((UINT8*)buffer_ptr)[byteOffset];
65 UINT8 mask1 = 0xff << (8-bitOffset);
66 mask1 = mask1 | (0xff >> bitOffset >> bitLength);
67 byte1 = (byte1 & mask1) | ((value >> bitOffset) & ~mask1);
68 ((UINT8*)buffer_ptr)[byteOffset] = byte1;
69 if(8-(int)bitOffset-(int)bitLength < 0) //need to set the second byte as well
70 {
71 UINT8 byte2 = ((UINT8*)buffer_ptr)[byteOffset+1];
72 UINT8 mask2 = 0xff >> (bitOffset+bitLength-8);
73 UINT8 value2 = value << (8-bitOffset);
74 byte2 = (byte2 & mask2) | (value2 & ~mask2);
75 ((UINT8*)buffer_ptr)[byteOffset+1] = byte2;
76 }
77 }
78 }
79
80 UINT16
81 ProtoPkt::GetUINT16Bits(unsigned int byteOffset, unsigned int bitOffset, unsigned int bitLength) const
82 {
83 if(bitLength<=8){
84 return (UINT16)GetUINT8Bits(byteOffset,bitOffset,bitLength);
85 } else {
86 return (((UINT16)GetUINT8Bits(byteOffset,bitOffset,8)) << (bitLength-8)) |
87 ((UINT16)GetUINT8Bits(byteOffset+1,bitOffset,bitLength-8));
88 }
89 }
90 void
91 ProtoPkt::SetUINT16Bits(unsigned int byteOffset,unsigned int bitOffset,UINT16 value, unsigned int bitLength)
92 {
93 if(bitLength<=8){
94 SetUINT8Bits(byteOffset,bitOffset,(UINT8)value,bitLength);
95 } else {
96 SetUINT8Bits(byteOffset,bitOffset,(UINT8)(value>>(bitLength-8)),8);
97 UINT8 mask = 0xff >> (16-bitLength);
98 SetUINT8Bits(byteOffset+1,bitOffset,(UINT8)(value) & mask,bitLength-8);
99 }
100 }
101 UINT32
102 ProtoPkt::GetUINT32Bits(unsigned int byteOffset, unsigned int bitOffset, unsigned int bitLength) const
103 {
104 if(bitLength <= 16)
105 {
106 return (UINT32)GetUINT16Bits(byteOffset,bitOffset,bitLength);
107 } else {
108 return (((UINT32)GetUINT16Bits(byteOffset,bitOffset,16)) << (bitLength-16)) |
109 ((UINT32)GetUINT16Bits(byteOffset+2,bitOffset,bitLength-16));
110 }
111 }
112 void
113 ProtoPkt::SetUINT32Bits(unsigned int byteOffset,unsigned int bitOffset,UINT32 value, unsigned int bitLength)
114 {
115 if(bitLength<=16){
116 SetUINT16Bits(byteOffset,bitOffset,(UINT16)value,bitLength);
117 } else {
118 SetUINT16Bits(byteOffset,bitOffset,(UINT16)(value>>(bitLength-16)),16);
119 UINT16 mask = 0xffff >> (32-bitLength);
120 SetUINT16Bits(byteOffset+2,bitOffset,(UINT16)(value) & mask,bitLength-16);
121 }
122 }