/*************************************************************************** binbuffer.cpp - binary buffer class ------------------- begin : di dec 17 2002 copyright : (C) 2002 by CJP email : cornware-cjp@users.sourceforge.net ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include #include "binbuffer.h" //Code coming from binbuffer.h //Originally written by bones /* CBinBuffer & CBinBuffer::operator += (const char unsigned & c) { this->push_back((Uint8) c); return (*this); } */ CBinBuffer & CBinBuffer::operator += (const char unsigned * c) { // c-string for (int i=0; c[i] != 0; i++) { this->push_back((Uint8) c[i]); } return (*this); } CBinBuffer & CBinBuffer::operator = (const char unsigned * c) { this->empty(); (*this)+=c; return (*this); } CBinBuffer & CBinBuffer::operator += (const CBinBuffer & bb) { for (int unsigned i=0;ipush_back(bb[i]); // FIXME: slow return (*this); } CBinBuffer CBinBuffer::getBuffer(int unsigned &pos, unsigned int maxlen) const { unsigned int s = size(); if(s==0) return CBinBuffer(); if(maxlen >= s) maxlen = s-1; CBinBuffer ret; ret.resize(maxlen); for (int unsigned i=0;ipush_back(i); return *this; } Uint8 CBinBuffer::getUint8(int unsigned &pos) const { if (pos > this->size()-1) throw(eEndOfBuffer); Uint8 ret = (Uint8) (*this)[pos]; pos++; return ret; } //Uint16: CBinBuffer & CBinBuffer::operator += (const Uint16 & i) { operator+=(HIBYTE(i)); operator+=(LOBYTE(i)); return *this; } Uint16 CBinBuffer::getUint16(int unsigned &pos) const { Uint16 ret = ((Uint16)getUint8(pos)) << 8; ret += ((Uint16)getUint8(pos)); return ret; } //Uint32: CBinBuffer & CBinBuffer::operator += (const Uint32 & i) { operator+=((Uint8)((i & 0xFF000000) >> 24)); operator+=((Uint8)((i & 0x00FF0000) >> 16)); operator+=((Uint8)((i & 0x0000FF00) >> 8 )); operator+=((Uint8)( i & 0x000000FF )); return *this; } Uint32 CBinBuffer::getUint32(int unsigned &pos) const { Uint32 ret = ((Uint32)getUint8(pos)) << 24; ret += ((Uint32)getUint8(pos)) << 16; ret += ((Uint32)getUint8(pos)) << 8; ret += ((Uint32)getUint8(pos)); return ret; } //String: CBinBuffer & CBinBuffer::operator += (const CString & bs) { operator+=((Uint8) bs.size()); for (int unsigned i=0;i this->size()-1) return ""; //throw(eEndOfBuffer); Uint8 ssize = getUint8(pos); if ((pos + ssize) > this->size()) return ""; //throw(eEndOfBuffer); for (int i=0;isize();i++) { char c=(*this)[i]; char s[10]; if ( ((int (c) >= 97) && (int (c) <= 122)) || ((int (c) >= 65) && (int (c) <= 90)) ) { sprintf(s, "('%c' 0x%x)", c, (Uint8) c); } else { sprintf(s, "('?' 0x%x)", (Uint8) c); } res+=s; } return res; } CBinBuffer CBinBuffer::substr(const int unsigned pos, const int n) const { CBinBuffer res; if (pos <= size()) { int len = n; if (len == -1) len = size() - pos; for (int unsigned i = pos;isize()+1]; for (int unsigned i=0;isize();i++) { res[i]= (char) (*this)[i]; } return (res); } */