00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "cxutils/packet.h"
00047 #include <stdlib.h>
00048 #include <string>
00049 #include <string.h>
00050 #include <assert.h>
00051 #include <iostream>
00052 #include <iomanip>
00053 #include <fstream>
00054 #include "cxutils/fileio.h"
00055
00056 using namespace CxUtils;
00057
00058
00064 Packet::Wrapper::Wrapper() : mPacket(0)
00065 {
00066 }
00067
00068
00076 Packet::Wrapper::Wrapper(const Packet::Wrapper& wrapper) : mPacket(0)
00077 {
00078 *this = wrapper;
00079 }
00080
00081
00092 Packet::Wrapper::Wrapper(unsigned char* buffer, const unsigned int len) : mPacket(0)
00093 {
00094 if(buffer && len > 0)
00095 {
00096 mPacket = new Packet();
00097 mPacket->mpPacket = buffer;
00098 mPacket->mReserved = mPacket->mLength = len;
00099 mPacket->mWrappedPacketFlag = true;
00100 }
00101 }
00102
00103
00109 Packet::Wrapper::~Wrapper()
00110 {
00111 Clear();
00112 }
00113
00114
00127 bool Packet::Wrapper::Create(unsigned char* buffer, const unsigned int len)
00128 {
00129 if(mPacket)
00130 {
00131 mPacket->mpPacket = 0;
00132 mPacket->mReserved = mPacket->mLength = 0;
00133 mPacket->mWrappedPacketFlag = true;
00134 delete mPacket;
00135 }
00136 if(buffer && len > 0)
00137 {
00138 mPacket = new Packet();
00139 mPacket->mpPacket = buffer;
00140 mPacket->mReserved = mPacket->mLength = len;
00141 mPacket->mWrappedPacketFlag = true;
00142 return true;
00143 }
00144 return false;
00145 }
00146
00147
00153 void Packet::Wrapper::Clear()
00154 {
00155 if(mPacket)
00156 {
00157 mPacket->mpPacket = 0;
00158 mPacket->mReserved = mPacket->mLength = 0;
00159 mPacket->mWrappedPacketFlag = true;
00160 delete mPacket;
00161 }
00162 mPacket = NULL;
00163 }
00164
00165
00172 const Packet* Packet::Wrapper::operator ->() const
00173 {
00174 return ((const Packet*)(mPacket));
00175 }
00176
00177
00184 Packet* Packet::Wrapper::operator ->()
00185 {
00186 return ((Packet*)(mPacket));
00187 }
00188
00195 const Packet* Packet::Wrapper::GetData() const
00196 {
00197 return ((const Packet*)(mPacket));
00198 }
00199
00200
00207 Packet* Packet::Wrapper::GetData()
00208 {
00209 return ((Packet*)(mPacket));
00210 }
00211
00212
00218 Packet::Wrapper& Packet::Wrapper::operator=(const Packet::Wrapper& wrapper)
00219 {
00220 if(this != &wrapper)
00221 {
00222 if(mPacket)
00223 {
00224 mPacket->mpPacket = 0;
00225 mPacket->mReserved = mPacket->mLength = 0;
00226 mPacket->mWrappedPacketFlag = true;
00227 delete mPacket;
00228 }
00229 if(wrapper.mPacket)
00230 {
00231 mPacket = new Packet();
00232 mPacket->mpPacket = wrapper.mPacket->mpPacket;
00233 mPacket->mReserved = mPacket->mLength = wrapper.mPacket->mLength;
00234 mPacket->mWrappedPacketFlag = true;
00235 }
00236 }
00237 return *this;
00238 }
00239
00240
00248 Packet::Packet(const unsigned int border)
00249 {
00250 mpPacket = NULL;
00251 mByteOrder = CX_PACKET_LITTLE_ENDIAN;
00252 if(border == CX_PACKET_BIG_ENDIAN)
00253 mByteOrder = border;
00254
00255 mLength = mReserved = mWritePos = mReadPos = 0;
00256 mWrappedPacketFlag = false;
00257 }
00258
00264 Packet::Packet(const Packet& another)
00265 {
00266 mpPacket = NULL;
00267 mLength = mReserved = mWritePos = mReadPos = 0;
00268 mWrappedPacketFlag = false;
00269 *this = another;
00270 }
00271
00277 Packet::~Packet()
00278 {
00279 Destroy();
00280 }
00281
00282
00301 int Packet::SetByteOrder(const unsigned int order)
00302 {
00303 if(order == CX_PACKET_LITTLE_ENDIAN || order == CX_PACKET_BIG_ENDIAN)
00304 {
00305 mByteOrder = order;
00306 return CX_PACKET_OK;
00307 }
00308 return CX_PACKET_FAILURE;
00309 }
00310
00311
00321 int Packet::SetWritePos(const unsigned int pos)
00322 {
00323 unsigned int *p = (unsigned int *)(&mWritePos);
00324 if(pos <= mLength)
00325 {
00326 *p = pos;
00327 return CX_PACKET_OK;
00328 }
00329 return CX_PACKET_FAILURE;
00330 }
00331
00332
00343 int Packet::SetReadPos(const unsigned int pos) const
00344 {
00345 unsigned int *p = (unsigned int *)(&mReadPos);
00346 if(pos <= mLength)
00347 {
00348 *p = pos;
00349 return CX_PACKET_OK;
00350 }
00351 return CX_PACKET_FAILURE;
00352 }
00353
00354
00377 int Packet::SetLength(const unsigned int len)
00378 {
00379 if(mpPacket && len <= mReserved)
00380 {
00381 mLength = len;
00382 mWritePos = mReadPos = 0;
00383 return CX_PACKET_OK;
00384 }
00385 return CX_PACKET_FAILURE;
00386 }
00387
00388
00404 int Packet::Insert(const unsigned char *buff, const unsigned int len, const unsigned int pos)
00405 {
00406 if(mLength == 0)
00407 return Write(buff, len, pos);
00408
00409 if(!buff || pos > mLength)
00410 return CX_PACKET_FAILURE;
00411
00412
00413 if(mLength + len >= mReserved)
00414 GrowPacket(mLength + len);
00415
00416
00417 memmove(&mpPacket[pos + len], &mpPacket[pos], sizeof(unsigned char)*(mLength - pos));
00418
00419 memcpy(&mpPacket[pos], buff, sizeof(unsigned char)*len);
00420 mLength += len;
00421 mpPacket[mLength] = 0;
00422 mReadPos = 0;
00423 mWritePos = mLength;
00424 return CX_PACKET_OK;
00425 }
00426
00427
00446 int Packet::InsertCharacter(const unsigned char value,
00447 const unsigned int len,
00448 const unsigned int pos)
00449 {
00450 if(mLength == 0)
00451 {
00452 if( mReserved < len )
00453 {
00454 GrowPacket( len + 1 );
00455 }
00456 memset( mpPacket, value, len );
00457 mLength = len;
00458 mWritePos = mLength;
00459 mReadPos = 0;
00460 return CX_PACKET_OK;
00461 }
00462
00463 if(pos > mLength)
00464 return CX_PACKET_FAILURE;
00465
00466
00467 if(mLength + len >= mReserved)
00468 GrowPacket(mLength + len);
00469
00470
00471 memmove(&mpPacket[pos + len], &mpPacket[pos], sizeof(unsigned char)*(mLength - pos));
00472
00473 memset( &mpPacket[pos], value, sizeof(unsigned char)*len );
00474 mLength += len;
00475 mpPacket[mLength] = 0;
00476 mReadPos = 0;
00477 mWritePos = mLength;
00478
00479 return CX_PACKET_OK;
00480 }
00481
00495 int Packet::Delete(const unsigned int len, unsigned int start)
00496 {
00497 int result = CX_PACKET_FAILURE;
00498 if(start < mLength && start + len <= mLength)
00499 {
00500 if(mpPacket)
00501 {
00502 if(len == mLength && start == 0)
00503 {
00504 mLength = 0;
00505 }
00506 else
00507 {
00508 memmove(&mpPacket[start], &mpPacket[start + len], sizeof(unsigned char)*(mLength - (start + len)));
00509 mLength -= len;
00510 }
00511 mpPacket[mLength] = 0;
00512 mWritePos = mReadPos = 0;
00513 result = CX_PACKET_OK;
00514 }
00515 }
00516
00517 return result;
00518 }
00519
00520
00526 unsigned int Packet::GetWritePos() const
00527 {
00528 return mWritePos;
00529 }
00530
00536 unsigned int Packet::GetReadPos() const
00537 {
00538 return mReadPos;
00539 }
00540
00555 unsigned int Packet::GetByteOrder() const
00556 {
00557 return mByteOrder;
00558 }
00559
00565 unsigned int Packet::Size() const { return mLength; }
00566
00572 unsigned int Packet::Length() const { return mLength; }
00573
00583 void Packet::Reserve(const unsigned int size)
00584 {
00585 if(mWrappedPacketFlag)
00586 {
00587 return;
00588 }
00589 assert(size > 0);
00590 if(mReserved >= size)
00591 return;
00592
00593 unsigned char *newPtr = new unsigned char[size];
00594 assert(newPtr);
00595
00596 memset(newPtr, 0, sizeof(unsigned char)*size);
00597 if(mLength > 0)
00598 {
00599
00600 if(mReserved < size && mpPacket)
00601 memcpy(newPtr, mpPacket, sizeof(unsigned char)*mReserved);
00602 else {
00603 mLength = mReadPos = mWritePos = 0;
00604 }
00605 }
00606
00607 mReserved = size;
00608 if(mpPacket)
00609 {
00610 delete[] mpPacket;
00611 mpPacket = NULL;
00612 }
00613 mpPacket = newPtr;
00614 }
00615
00616
00631 int Packet::Write(const unsigned char *buff, const unsigned int len, const unsigned int pos)
00632 {
00633 assert(len > 0);
00634 bool overwrite = false;
00635 unsigned int epos = 0;
00636
00637
00638
00639 if(pos == UINT_MAX)
00640 {
00641 epos = mWritePos;
00642 }
00643 else
00644 {
00645 epos = pos;
00646 }
00647
00648
00649 if( epos > mLength || epos == UINT_MAX)
00650 {
00651 return 0;
00652 }
00653 if(epos + len >= mReserved && mWrappedPacketFlag)
00654 {
00655 return 0;
00656 }
00657 if(epos + len >= mReserved)
00658 GrowPacket(epos + len + 1);
00659 memcpy(&mpPacket[epos], buff, sizeof(unsigned char)*len);
00660
00661
00662
00663 if(!overwrite)
00664 mWritePos += len;
00665
00666 if (epos + len > mLength)
00667 {
00668 mLength = epos + len;
00669
00670 if(mLength < mReserved)
00671 {
00672 mpPacket[mLength] = '\0';
00673 }
00674 }
00675 return len;
00676 }
00677
00678
00700 int Packet::Write(const unsigned char val, const unsigned int pos)
00701 {
00702 return WritePacket(this, (unsigned char)(val), pos);
00703 }
00704
00705
00727 int Packet::Write(const char val, const unsigned int pos)
00728 {
00729 return WritePacket(this, (char)(val), pos);
00730 }
00731
00732
00754 int Packet::Write(const unsigned int val, const unsigned int pos)
00755 {
00756 return WritePacket(this, val, pos);
00757 }
00758
00759
00781 int Packet::Write(const unsigned short val, const unsigned int pos)
00782 {
00783 return WritePacket(this, val, pos);
00784 }
00785
00786
00808 int Packet::Write(const int val, const unsigned int pos)
00809 {
00810 return WritePacket(this, val, pos);
00811 }
00812
00813
00835 int Packet::Write(const short val, const unsigned int pos)
00836 {
00837 return WritePacket(this, val, pos);
00838 }
00839
00840
00862 int Packet::Write(const long long int val, const unsigned int pos)
00863 {
00864 return WritePacket(this, val, pos);
00865 }
00866
00867
00889 int Packet::Write(const unsigned long long int val, const unsigned int pos)
00890 {
00891 return WritePacket(this, val, pos);
00892 }
00893
00894
00916 int Packet::Write(const float val, const unsigned int pos)
00917 {
00918 int result;
00919 int temp;
00920 memcpy(&temp, &val, sizeof(val));
00921 result = WritePacket(this, temp, pos);
00922 return result;
00923 }
00924
00925
00947 int Packet::Write(const double val, const unsigned int pos)
00948 {
00949 int result;
00950 long long int temp;
00951 memcpy(&temp, &val, sizeof(val));
00952 result = WritePacket(this, temp, pos);
00953 return result;
00954 }
00955
00956
00978 int Packet::Write(const Packet& packet, const unsigned int pos)
00979 {
00980 return Write(packet.mpPacket, packet.mLength, pos);
00981 }
00982
00983
00996 int Packet::Write(const std::string& str, const unsigned int pos)
00997 {
00998 return Write((unsigned char *)(str.c_str()), (unsigned int)(str.size()), pos);
00999 }
01000
01001
01002
01013 void Packet::Clear(const bool setZeroFlag)
01014 {
01015 if(mpPacket && mReserved)
01016 {
01017 *mpPacket = 0;
01018 if(setZeroFlag)
01019 {
01020 memset(mpPacket, 0, sizeof(unsigned char)*mReserved);
01021 }
01022 }
01023 mWritePos = mReadPos = 0;
01024 mLength = 0;
01025 }
01026
01033 void Packet::Destroy()
01034 {
01035 if(mWrappedPacketFlag == false)
01036 {
01037 if(mpPacket)
01038 delete[] mpPacket;
01039 }
01040 mpPacket = 0;
01041 mWritePos = mReadPos = 0;
01042 mLength = mReserved = 0;
01043 }
01044
01045
01053 void Packet::Print(const unsigned int bytesPerLine) const
01054 {
01055 unsigned int b = 0;
01056 for(unsigned int i = 0; i < mLength; i++)
01057 {
01058 std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)mpPacket[i] << " ";
01059 if(b == bytesPerLine)
01060 {
01061 std::cout << std::endl;
01062 b = 0;
01063 }
01064 else
01065 {
01066 b++;
01067 }
01068 }
01069 std::cout << std::endl;
01070 }
01071
01072
01094 int Packet::Read(unsigned char& val, const unsigned int pos) const
01095 {
01096 return ReadPacket(this, val, pos);
01097 }
01098
01099
01121 int Packet::Read(char& val, const unsigned int pos) const
01122 {
01123 return ReadPacket(this, val, pos);
01124 }
01125
01126
01148 int Packet::Read(int& val, const unsigned int pos) const
01149 {
01150 return ReadPacket(this, val, pos);
01151 }
01152
01153
01175 int Packet::Read(short& val, const unsigned int pos) const
01176 {
01177 return ReadPacket(this, val, pos);
01178 }
01179
01180
01202 int Packet::Read(long long int& val, const unsigned int pos) const
01203 {
01204 return ReadPacket(this, val, pos);
01205 }
01206
01207
01229 int Packet::Read(unsigned int& val, const unsigned int pos) const
01230 {
01231 return ReadPacket(this, val, pos);
01232 }
01233
01234
01256 int Packet::Read(unsigned short& val, const unsigned int pos) const
01257 {
01258 return ReadPacket(this, val, pos);
01259 }
01260
01261
01283 int Packet::Read(unsigned long long int& val, const unsigned int pos) const
01284 {
01285 return ReadPacket(this, val, pos);
01286 }
01287
01288
01310 int Packet::Read(float& val, const unsigned int pos) const
01311 {
01312 int temp;
01313 int result = ReadPacket(this, temp, pos);
01314 if(result != 0)
01315 {
01316 memcpy(&val, &temp, sizeof(val));
01317 }
01318 return result;
01319 }
01320
01321
01343 int Packet::Read(double& val, const unsigned int pos) const
01344 {
01345 long long int temp;
01346 int result = ReadPacket(this, temp, pos);
01347 if(result != 0)
01348 {
01349 memcpy(&val, &temp, sizeof(val));
01350 }
01351 return result;
01352 }
01353
01373 int Packet::Read(unsigned char *buff, const unsigned int len, const unsigned int pos) const
01374 {
01375 bool useReadPos = false;
01376 unsigned int dpos = 0;
01377
01378 if(pos == UINT_MAX)
01379 {
01380 useReadPos = true;
01381 dpos = mReadPos;
01382 }
01383 else
01384 dpos = pos;
01385
01386 if(mpPacket && len + dpos <= mLength)
01387 {
01388 memcpy(buff, &mpPacket[dpos], len);
01389 if(useReadPos)
01390 {
01391 SetReadPos(mReadPos + len);
01392 }
01393 return len;
01394 }
01395
01396 return CX_PACKET_FAILURE;
01397 }
01398
01399
01418 int Packet::Read(Packet& packet, const unsigned int len, const unsigned int pos) const
01419 {
01420 bool useReadPos = false;
01421 unsigned int dpos = 0;
01422
01423 packet.Clear();
01424
01425 if(pos == UINT_MAX)
01426 {
01427 useReadPos = true;
01428 dpos = mReadPos;
01429 }
01430 else
01431 dpos = pos;
01432
01433 if(len + dpos <= mLength)
01434 {
01435 packet.Reserve(len + 2);
01436 if(Read(packet.mpPacket, len, pos) == (int)len)
01437 {
01438 packet.mLength = len;
01439 packet.mReadPos = packet.mWritePos = 0;
01440 return len;
01441 }
01442 }
01443
01444 return CX_PACKET_FAILURE;
01445 }
01446
01447
01465 int Packet::Read(std::string& str, const unsigned int len, const unsigned int pos) const
01466 {
01467 bool useReadPos = false;
01468 unsigned int dpos = 0;
01469
01470 str.clear();
01471 if(pos == UINT_MAX)
01472 {
01473 useReadPos = true;
01474 dpos = mReadPos;
01475 }
01476 else
01477 dpos = pos;
01478
01479 if(mpPacket && len + dpos <= mLength)
01480 {
01481 for(unsigned int i = dpos; i < dpos + len; i++)
01482 {
01483 str.push_back(mpPacket[i]);
01484 }
01485 if(useReadPos)
01486 {
01487 SetReadPos(mReadPos + len);
01488 }
01489 return len;
01490 }
01491
01492 return CX_PACKET_FAILURE;
01493 }
01494
01495
01515 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, unsigned char& val, const unsigned int border)
01516 {
01517 int result = 0;
01518 Packet p(border);
01519 assert(buff && len > 0 && pos < len);
01520 p.mpPacket = (unsigned char *)buff;
01521 p.mLength = len;
01522 p.mReserved = len;
01523 result = p.Read(val, pos);
01524 p.mpPacket = NULL;
01525 p.mLength = 0;
01526 p.mReserved = 0;
01527 return result;
01528 }
01529
01530
01550 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, char& val, const unsigned int border)
01551 {
01552 int result = 0;
01553 Packet p(border);
01554 assert(buff && len > 0 && pos < len);
01555 p.mpPacket = (unsigned char *)buff;
01556 p.mLength = len;
01557 p.mReserved = len;
01558 result = p.Read(val, pos);
01559 p.mpPacket = NULL;
01560 p.mLength = 0;
01561 p.mReserved = 0;
01562 return result;
01563 }
01564
01565
01585 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, int& val, const unsigned int border)
01586 {
01587 int result = 0;
01588 Packet p(border);
01589 assert(buff && len > 0 && pos < len);
01590 p.mpPacket = (unsigned char *)buff;
01591 p.mLength = len;
01592 p.mReserved = len;
01593 result = p.Read(val, pos);
01594 p.mpPacket = NULL;
01595 p.mLength = 0;
01596 p.mReserved = 0;
01597 return result;
01598 }
01599
01600
01620 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, unsigned int& val, const unsigned int border)
01621 {
01622 int result = 0;
01623 Packet p(border);
01624 assert(buff && len > 0 && pos < len);
01625 p.mpPacket = (unsigned char *)buff;
01626 p.mLength = len;
01627 p.mReserved = len;
01628 result = p.Read(val, pos);
01629 p.mpPacket = NULL;
01630 p.mLength = 0;
01631 p.mReserved = 0;
01632 return result;
01633 }
01634
01635
01655 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, short& val, const unsigned int border)
01656 {
01657 int result = 0;
01658 Packet p(border);
01659 assert(buff && len > 0 && pos < len);
01660 p.mpPacket = (unsigned char *)buff;
01661 p.mLength = len;
01662 p.mReserved = len;
01663 result = p.Read(val, pos);
01664 p.mpPacket = NULL;
01665 p.mLength = 0;
01666 p.mReserved = 0;
01667 return result;
01668 }
01669
01670
01690 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, unsigned short& val, const unsigned int border)
01691 {
01692 int result = 0;
01693 Packet p(border);
01694 assert(buff && len > 0 && pos < len);
01695 p.mpPacket = (unsigned char *)buff;
01696 p.mLength = len;
01697 p.mReserved = len;
01698 result = p.Read(val, pos);
01699 p.mpPacket = NULL;
01700 p.mLength = 0;
01701 p.mReserved = 0;
01702 return result;
01703 }
01704
01705
01725 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, long long int& val, const unsigned int border)
01726 {
01727 int result = 0;
01728 Packet p(border);
01729 assert(buff && len > 0 && pos < len);
01730 p.mpPacket = (unsigned char *)buff;
01731 p.mLength = len;
01732 p.mReserved = len;
01733 result = p.Read(val, pos);
01734 p.mpPacket = NULL;
01735 p.mLength = 0;
01736 p.mReserved = 0;
01737 return result;
01738 }
01739
01740
01760 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, unsigned long long int& val, const unsigned int border)
01761 {
01762 int result = 0;
01763 Packet p(border);
01764 assert(buff && len > 0 && pos < len);
01765 p.mpPacket = (unsigned char *)buff;
01766 p.mLength = len;
01767 p.mReserved = len;
01768 result = p.Read(val, pos);
01769 p.mpPacket = NULL;
01770 p.mLength = 0;
01771 p.mReserved = 0;
01772 return result;
01773 }
01774
01775
01795 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, float& val, const unsigned int border)
01796 {
01797 int result = 0;
01798 Packet p(border);
01799 assert(buff && len > 0 && pos < len);
01800 p.mpPacket = (unsigned char *)buff;
01801 p.mLength = len;
01802 p.mReserved = len;
01803 result = p.Read(val, pos);
01804 p.mpPacket = NULL;
01805 p.mLength = 0;
01806 p.mReserved = 0;
01807 return result;
01808 }
01809
01810
01830 int Packet::Read(const unsigned char *buff, const unsigned int len, const unsigned int pos, double& val, const unsigned int border)
01831 {
01832 int result = 0;
01833 Packet p(border);
01834 assert(buff && len > 0 && pos < len);
01835 p.mpPacket = (unsigned char *)buff;
01836 p.mLength = len;
01837 p.mReserved = len;
01838 result = p.Read(val, pos);
01839 p.mpPacket = NULL;
01840 p.mLength = 0;
01841 p.mReserved = 0;
01842 return result;
01843 }
01844
01845
01851 const unsigned char *Packet::Ptr() const
01852 {
01853 return mpPacket;
01854 }
01855
01856
01862 unsigned char *Packet::Ptr()
01863 {
01864 return mpPacket;
01865 }
01866
01867
01873 Packet& Packet::operator=(const Packet& another)
01874 {
01875 if(this != &another && !mWrappedPacketFlag)
01876 {
01877 if(mLength < another.mLength && another.mLength > 0)
01878 {
01879 Reserve(another.mLength + 1);
01880 }
01881 if(another.mLength > 0 && another.mpPacket)
01882 {
01883
01884 if(!mpPacket)
01885 {
01886 Reserve(another.mLength + 1);
01887 }
01888 memcpy(mpPacket, another.mpPacket, sizeof(unsigned char)*another.mLength);
01889 }
01890 mLength = another.mLength;
01891 mReadPos = another.mReadPos;
01892 mWritePos = another.mWritePos;
01893 mByteOrder = another.mByteOrder;
01894 }
01895 return *this;
01896 }
01897
01903 Packet& Packet::operator +=(const Packet& another)
01904 {
01905 Write(another, mLength);
01906 return *this;
01907 }
01908
01909
01919 unsigned char Packet::operator [](const unsigned int index) const
01920 {
01921 if(index < mLength && mpPacket)
01922 {
01923 return mpPacket[index];
01924 }
01925 assert("CxUtils::Packet - Out of Range <operator[]>");
01926 return 0;
01927 }
01928
01929
01937 void Packet::GrowPacket(const unsigned int size)
01938 {
01939 if(mWrappedPacketFlag == false)
01940 {
01941 unsigned int newSize = mReserved;
01942 if(size >= UINT_MAX)
01943 return;
01944
01945 while(newSize < size + 1 && newSize + CX_PACKET_BLOCK_SIZE < UINT_MAX)
01946 newSize += CX_PACKET_BLOCK_SIZE;
01947 if(newSize > mReserved)
01948 Reserve(newSize);
01949 }
01950 }
01951
01952
01968 bool Packet::LoadWiresharkCapturePacketExport(const std::string &filename, Packet::List &packets)
01969 {
01970 bool result = false;
01971
01972 packets.clear();
01973
01974 std::fstream infile;
01975 infile.open(filename.c_str());
01976 if(infile.is_open() == false)
01977 {
01978 return result;
01979 }
01980 Packet p;
01981 std::vector<std::string> lines, tokens;
01982 std::string l;
01983 while(infile.eof() == false)
01984 {
01985 CxUtils::FileIO::ReadLine(infile, l);
01986 if(l.size() > 0)
01987 {
01988 lines.clear();
01989 lines.push_back(l);
01990 while(CxUtils::FileIO::ReadLine(infile, l))
01991 {
01992 lines.push_back(l);
01993 }
01994
01995 p.Clear();
01996 unsigned long ul = 0;
01997 std::vector<std::string>::iterator d;
01998 for(d = lines.begin();
01999 d != lines.end();
02000 d++)
02001 {
02002
02003 ul = 0;
02004 tokens = CxUtils::FileIO::Tokenize(*d, " ");
02005 for(unsigned int i = 2; i < 18 && i < tokens.size(); i++)
02006 {
02007 if(tokens[i].size() < 1)
02008 {
02009 break;
02010 }
02011 ul = strtoul(tokens[i].c_str(), NULL, 16);
02012 p.WriteByte((unsigned char)ul);
02013 result = true;
02014 }
02015 }
02016 packets.push_back(p);
02017 }
02018 }
02019
02020 return result;
02021 }
02022
02023
02024