00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef BUFFER_H
00023 #define BUFFER_H
00024
00025 #include <vector>
00026 #include <iostream>
00027 #include <iomanip.h>
00028 #include <string>
00029 #include <iterator>
00030
00031 #include <libicq2000/Translator.h>
00032
00033 using std::vector;
00034 using std::string;
00035 using std::ostream;
00036
00037 using ICQ2000::Translator;
00038
00039 class Buffer {
00040 public:
00041 typedef unsigned int size_type;
00042
00043 enum endian { BIG, LITTLE };
00044
00045 struct marker {
00046 size_type position;
00047 endian endianness;
00048 int size;
00049 };
00050
00051 private:
00052 typedef vector<unsigned char>::iterator iterator;
00053
00054 vector<unsigned char> m_data;
00055 endian m_endn;
00056 size_type m_out_pos;
00057 Translator *m_translator;
00058
00059 public:
00060 Buffer(Translator *translator);
00061 Buffer(const unsigned char *d, int size, Translator *translator);
00062
00063 Buffer(Buffer& b, unsigned int start, unsigned int data_len);
00064
00065 unsigned int size() const { return m_data.size(); }
00066 unsigned int pos() const { return m_out_pos; }
00067 unsigned int remains() const { return m_data.size() - m_out_pos; }
00068
00069 iterator begin() { return m_data.begin(); }
00070 iterator end() { return m_data.end(); }
00071
00072 void clear();
00073 bool empty();
00074 void advance(unsigned int ad) { m_out_pos += ad; }
00075 bool beforeEnd() const { return (m_out_pos < m_data.size()); }
00076 void setPos(unsigned int o) { m_out_pos = o; }
00077 void chopOffBuffer(Buffer& b, unsigned int sz);
00078
00079 void setEndianness(endian e);
00080 void setBigEndian();
00081 void setLittleEndian();
00082
00083 marker getAutoSizeShortMarker();
00084 marker getAutoSizeIntMarker();
00085 void setAutoSizeMarker(const marker& m);
00086
00087 Buffer& operator<<(unsigned char);
00088 Buffer& operator<<(unsigned short);
00089 Buffer& operator<<(unsigned int);
00090 Buffer& operator<<(signed char l) { return (*this) << (unsigned char)l; }
00091 Buffer& operator<<(signed short l) { return (*this) << (unsigned short)l; }
00092 Buffer& operator<<(signed int l) { return (*this) << (unsigned int)l; }
00093 Buffer& operator<<(const string&);
00094
00095 Buffer& operator>>(unsigned char&);
00096 Buffer& operator>>(unsigned short&);
00097 Buffer& operator>>(unsigned int&);
00098 Buffer& operator>>(string&);
00099
00100 void Pack(const unsigned char *d, int size);
00101 void Pack(const string& s);
00102 void PackUint16StringNull(const string& s);
00103 void PackByteString(const string& s);
00104
00105 void Unpack(string& s, int size);
00106 void Unpack(unsigned char *const d, int size);
00107 unsigned char UnpackChar();
00108 void UnpackUint32String(string& s);
00109 void UnpackUint16StringNull(string& s);
00110 void UnpackByteString(string& s);
00111
00112 unsigned char& operator[](unsigned int p);
00113
00114 void setTranslator(Translator *translator);
00115 void ServerToClient(string& szString);
00116 void ClientToServer(string& szString);
00117 void ServerToClient(char &_cChar);
00118 void ClientToServer(char &_cChar);
00119
00120 void dump(ostream& out);
00121 };
00122
00123 ostream& operator<<(ostream&,Buffer&);
00124
00125 #endif