blob: 2255b370c0201df1ab07fa141e6504438cca7c59 [file] [log] [blame]
anjana_sreekumar@infosys.com991c2062020-01-08 11:42:57 +05301/*
2 * msgBuffer.cpp
3 *
4 * Created on: Dec 2012
5 * Author: hariharanb
6 */
7
8#include <arpa/inet.h>
9#include <stdio.h>
10#include <string.h>
11#include <basicTypes.h>
12#include <msgBuffer.h>
13
14using namespace std;
15
16using namespace cmn::utils;
17
18MsgBuffer::MsgBuffer()
19{
20 initialize(DEFAULT_BUFF_SIZE);
21}
22
23MsgBuffer::MsgBuffer(uint16_t size)
24{
25 initialize(size);
26}
27
28MsgBuffer::~MsgBuffer()
29{
30 delete data_mp;
31}
32
33void MsgBuffer::initialize(uint16_t size)
34{
35 bufSize = size;
36 data_mp = new uint8_t[size];
37 memset(data_mp, 0, size);
38 bitIndex = 0;
39 byteIndex = 0;
40 length = 0;
41 bitLength = 0;
42}
43
44bool MsgBuffer::writeBits(uint8_t data, uint8_t size, bool append)
45{
46
47 uint8_t mask = (0xFF >> (8 - size));
48 data = data & mask;
49
50 if ((bitIndex + size) <= 8)
51 {
52 if (append)
53 {
54 goToEnd();
55 }
56
57 data_mp[byteIndex] = data_mp[byteIndex] | (data << (8-(bitIndex+size)));
58 incrBitIndex(size);
59
60 return true;
61
62 }
63 else
64 {
65 // Attempt to write beyond byte boundary
66 return false;
67 }
68}
69
70bool MsgBuffer::writeBytes(uint8_t* data, uint16_t size, bool append)
71{
72 bool rc = false;
73 if (bitIndex == 0)
74 {
75 if (append)
76 {
77 goToEnd();
78 }
79
80 if ((byteIndex + size) <= bufSize)
81 {
82 memcpy(&data_mp[byteIndex], data, size);
83 rc = incrByteIndex(size);
84 if (byteIndex > length)
85 {
86 length = byteIndex;
87 }
88 }
89 }
90 return rc;
91}
92
93bool MsgBuffer::writeUint8(uint8_t data, bool append)
94{
95 return writeBytes(&data, sizeof(uint8_t), append);
96}
97
98bool MsgBuffer::writeUint16(uint16_t data, bool append)
99{
100 uint16_t localData = htons(data);
101 return writeBytes((uint8_t*)&localData, sizeof(uint16_t), append);
102}
103
104bool MsgBuffer::writeUint32(uint32_t data, bool append)
105{
106 uint32_t localData = htonl(data);
107 return writeBytes((uint8_t*)&localData, sizeof(uint32_t), append);
108}
109
110bool MsgBuffer::writeUint64(uint64_t data, bool append)
111{
112 uint64_t localData = htonl(data);
113 return writeBytes((uint8_t*)&localData, sizeof(uint64_t), append);
114}
115
116bool MsgBuffer::readBit()
117{
118 uint8_t byteValue = data_mp[byteIndex];
119 uint8_t bitMask = 0x80;
120 bitMask = bitMask >> bitIndex;
121
122 // Adjust the indices
123 incrBitIndex(1);
124
125 if (byteValue & bitMask)
126 {
127 return true;
128 }
129 else
130 {
131 return false;
132 }
133}
134
135void MsgBuffer::readUint8(uint8_t &data)
136{
137 data = data_mp[byteIndex];
138 nextByte();
139}
140
141bool MsgBuffer::readUint16(uint16_t &data)
142{
143 if ((byteIndex + sizeof(uint16_t)) <= length)
144 {
145 memcpy(&data, &data_mp[byteIndex], sizeof(uint16_t));
146 data = ntohs(data);
147 incrByteIndex(sizeof(uint16_t));
148 return true;
149 }
150 else
151 {
152 return false;
153 }
154}
155
156bool MsgBuffer::readUint32(uint32_t &data)
157{
158 if ((byteIndex + sizeof(uint32_t)) <= length)
159 {
160 memcpy(&data, &data_mp[byteIndex], sizeof(uint32_t));
161 data = ntohl(data);
162 incrByteIndex(sizeof(uint32_t));
163 return true;
164 }
165 else
166 {
167 return false;
168 }
169}
170
171bool MsgBuffer::readUint64(uint64_t &data)
172{
173 if ((byteIndex + sizeof(uint64_t)) <= length)
174 {
175 memcpy(&data, &data_mp[byteIndex], sizeof(uint64_t));
176 data = ntohl(data);
177 incrByteIndex(sizeof(uint64_t));
178 return true;
179 }
180 else
181 {
182 return false;
183 }
184}
185
186uint8_t MsgBuffer::readBits(uint16_t size)
187{
188 uint8_t data = 0;
189
190 if ((bitIndex + size) > 8)
191 {
192 cout << "Attempt to read beyond byte boundary";
193 return 0;
194 }
195
196 uint16_t i;
197 for (i = 0; i <size; i++)
198 {
199 data = data << 1;
200 if (readBit())
201 {
202 data = data | 0x0001;
203 }
204 }
205 return data;
206}
207
208
209bool MsgBuffer::readBytes(uint8_t* data, uint16_t size)
210{
211 if ((byteIndex + size) <= length)
212 {
213 memcpy(data, &data_mp[byteIndex], size);
214 byteIndex += size;
215 bitIndex = 0;
216 return true;
217 }
218 else
219 {
220 return false;
221 }
222
223
224}
225
226void MsgBuffer::rewind()
227{
228 bitIndex = 0;
229 byteIndex = 0;
230}
231
232void MsgBuffer::reset()
233{
234 memset(data_mp, 0, length);
235 rewind();
236 length = 0;
237 bitLength = 0;
238}
239
240void MsgBuffer::skipBits(uint8_t size)
241{
242 incrBitIndex(size);
243}
244
245void MsgBuffer::skipBytes(uint16_t size)
246{
247 incrByteIndex(size);
248}
249
250void MsgBuffer::display(Debug &stream)
251{
252
253}
254
255bool MsgBuffer::incrBitIndex(uint8_t size)
256{
257 bool rc = false;
258 if ((bitIndex + size) <= 8)
259 {
260 uint16_t savedBitIndex = bitIndex;
261 bitIndex += size;
262 rc = true;
263 if (bitIndex == 8)
264 {
265 rc = incrByteIndex(1);
266 bitIndex = 0;
267 }
268
269 if (!rc)
270 {
271 bitIndex = savedBitIndex;
272 }
273 }
274 // adjust the length now
275 if ((byteIndex == length)&&(bitIndex > bitLength))
276 {
277 bitLength = bitIndex;
278 }
279 else if (byteIndex > length)
280 {
281 length = byteIndex;
282 bitLength = bitIndex;
283 }
284 return rc;
285}
286
287bool MsgBuffer::incrByteIndex(uint16_t size)
288{
289 if ((byteIndex + size) <= bufSize)
290 {
291 byteIndex += size;
292 return true;
293 }
294 else
295 {
296 return false;
297 }
298}
299
300void MsgBuffer::nextByte()
301{
302 byteIndex++;
303 if (byteIndex > length)
304 {
305 byteIndex = 0;
306 }
307 bitIndex = 0;
308}
309
310void MsgBuffer::goToEnd()
311{
312 byteIndex = length;
313 bitIndex = bitLength;
314}
315
316uint16_t MsgBuffer::getLength()
317{
318 if (bitIndex == 0)
319 return length;
320 else
321 return (length + 1);
322}
323
324uint16_t MsgBuffer::getBufferSize()
325{
326 return bufSize;
327}
328
329uint16_t MsgBuffer::getCurrentIndex()
330{
331 return byteIndex;
332}
333
334void MsgBuffer::goToIndex(uint16_t idx)
335{
336 byteIndex = idx;
337}
338
339uint16_t MsgBuffer::lengthLeft()
340{
341 return (length - byteIndex);
342}
343
344uint16_t MsgBuffer::sizeLeft()
345{
346 return (bufSize - byteIndex);
347}
348
349void* MsgBuffer::getDataPointer()
350{
351 return data_mp;
352}
353
354void MsgBuffer::setLength(uint16_t bufLen)
355{
356 length = bufLen;
357}
358
359uint8_t MsgBuffer::charToHex (uint8_t x)
360{
361 if ((x >= '0') && (x <= '9'))
362 {
363 return (x - '0');
364 }
365 else if ((x >= 'a') && (x <= 'f'))
366 {
367 return (x - 'a' + 10);
368 }
369 else if ((x >= 'A') && (x <= 'F'))
370 {
371 return (x - 'A' + 10);
372 }
373 else
374 {
375 return 0;
376 }
377}
378