1 /* 2 * Buffer class header 3 * 4 * Copyright (C) 2001 Barnaby Gray <barnaby@beedesign.co.uk>. 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 */ 21 22 #ifndef BUFFER_H 23 #define BUFFER_H 24 25 #include <vector> 26 #include <iostream> 27 #include <iomanip> 28 #include <string> 29 #include <iterator> 30 31 namespace ICQ2000 32 { 33 34 class Buffer 35 { 36 public: 37 typedef unsigned int size_type; 38 39 enum endian { BIG, LITTLE }; 40 41 struct marker { 42 size_type position; 43 endian endianness; 44 int size; 45 }; 46 47 private: 48 typedef std::vector<unsigned char>::iterator iterator; 49 50 std::vector<unsigned char> m_data; 51 endian m_endn; 52 size_type m_out_pos; 53 54 public: 55 Buffer(); 56 Buffer(const unsigned char *d, unsigned int size); // construct from an array 57 Buffer(Buffer& b, unsigned int start, unsigned int data_len); // construct by copying from another Buffer 58 size()59 unsigned int size() const { return m_data.size(); } pos()60 unsigned int pos() const { return m_out_pos; } remains()61 unsigned int remains() const { return m_data.size() - m_out_pos; } 62 begin()63 iterator begin() { return m_data.begin(); } end()64 iterator end() { return m_data.end(); } 65 66 void clear(); 67 bool empty(); advance(unsigned int ad)68 void advance(unsigned int ad) { m_out_pos += ad; } beforeEnd()69 bool beforeEnd() const { return (m_out_pos < m_data.size()); } setPos(unsigned int o)70 void setPos(unsigned int o) { m_out_pos = o; } 71 void chopOffBuffer(Buffer& b, unsigned int sz); 72 73 void setEndianness(endian e); 74 void setBigEndian(); 75 void setLittleEndian(); 76 77 marker getAutoSizeShortMarker(); 78 marker getAutoSizeIntMarker(); 79 void setAutoSizeMarker(const marker& m); 80 81 Buffer& operator<<(unsigned char); 82 Buffer& operator<<(unsigned short); 83 Buffer& operator<<(unsigned int); 84 Buffer& operator<<(signed char l) { return (*this) << (unsigned char)l; } 85 Buffer& operator<<(signed short l) { return (*this) << (unsigned short)l; } 86 Buffer& operator<<(signed int l) { return (*this) << (unsigned int)l; } 87 Buffer& operator<<(const std::string&); 88 89 Buffer& operator>>(unsigned char&); 90 Buffer& operator>>(unsigned short&); 91 Buffer& operator>>(unsigned int&); 92 Buffer& operator>>(signed char& l) { return (*this) >> (unsigned char&)l; } 93 Buffer& operator>>(signed short& l) { return (*this) >> (unsigned short&)l; } 94 Buffer& operator>>(signed int& l) { return (*this) >> (unsigned int&)l; } 95 Buffer& operator>>(std::string&); 96 97 void Pack(const unsigned char *d, unsigned int size); 98 void Pack(const std::string& s); 99 void PackUint16StringNull(const std::string& s); 100 void PackByteString(const std::string& s); 101 void UnpackCRLFString(std::string& s); 102 103 void Unpack(std::string& s, unsigned int size); 104 void Unpack(unsigned char *const d, unsigned int size); 105 unsigned char UnpackChar(); 106 void UnpackUint32String(std::string& s); 107 void UnpackUint16StringNull(std::string& s); 108 void UnpackByteString(std::string& s); 109 110 unsigned char& operator[](unsigned int p); 111 112 void dump(std::ostream& out); 113 }; 114 115 std::ostream& operator<<(std::ostream&,Buffer&); 116 117 } 118 119 #endif 120