/*! @file pibytearray.h * @brief Byte array */ /* PIP - Platform Independent Primitives Byte array Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #ifndef PIBYTEARRAY_H #define PIBYTEARRAY_H #include "pichar.h" #include "pibitarray.h" #include "pimap.h" #include "pivector2d.h" #include #ifdef MICRO_PIP # define _TYPENAME_(T) "?" #else # define _TYPENAME_(T) typeid(T).name() #endif class PIString; class PIByteArray; /*! @class PIByteArray * @brief The PIByteArray class provides an array of bytes * @details PIByteArray used to store raw bytes. * It can be constructed from any data and size. * You can use PIByteArray as binary stream * to serialize/deserialize any objects and data. * This class based on PIDeque and provide some handle function * to manipulate it. * * @section PIByteArray_sec0 Usage * %PIByteArray can be used to store custom data and manipulate it. There are many * stream operators to store/restore common types to byte array. Store operators * places data at the end of array, restore operators takes data from the beginning * of array. * In addition there are Hex and Base64 convertions * * One of the major usage of %PIByteArray is stream functions. You can form binary * packet from many types (also dynamic types, e.g. PIVector) with one line: * @snippet pibytearray.cpp 0 * * Or you can descibe stream operator of your own type and store/restore vectors of * your type: * @snippet pibytearray.cpp 1 * * For store/restore custom data blocks there is PIByteArray::RawData class. Stream * operators of this class simply store/restore data block to/from byte array. * @snippet pibytearray.cpp 2 * * @section PIByteArray_sec1 Attention * Stream operator of %PIByteArray store byte array as vector, not simply append * content of byte array. This operators useful to transmit custom data as %PIByteArray * packed into parent byte array, e.g. to form packet from %PIByteArray. * To append one byte array to another use funtion \a append(). * @snippet pibytearray.cpp 3 * * */ class PIP_EXPORT PIByteArray: public PIDeque { public: //! Constructs an empty byte array PIByteArray() {;} PIByteArray(const PIByteArray & o): PIDeque(o) {} PIByteArray(const PIDeque & o): PIDeque(o) {} PIByteArray(PIByteArray && o): PIDeque(std::move(o)) {} //! Constructs 0-filled byte array with size "size" PIByteArray(const uint size) {resize(size);} //! Constructs byte array from data "data" and size "size" PIByteArray(const void * data, const uint size): PIDeque((const uchar*)data, size_t(size)) {} //! Constructs byte array with size "size" filled by "t" PIByteArray(const uint size, uchar t): PIDeque(size, t) {} //! Help struct to store/restore custom blocks of data to/from PIByteArray struct RawData { friend PIByteArray & operator <<(PIByteArray & s, const PIByteArray::RawData & v); friend PIByteArray & operator >>(PIByteArray & s, PIByteArray::RawData v); public: //! Constructs data block RawData(void * data = 0, int size = 0) {d = data; s = size;} RawData(const RawData & o) {d = o.d; s = o.s;} //! Constructs data block RawData(const void * data, const int size) {d = const_cast(data); s = size;} RawData & operator =(const RawData & o) {d = o.d; s = o.s; return *this;} private: void * d; int s; }; //! Return resized byte array PIByteArray resized(uint new_size) const {PIByteArray ret(new_size); memcpy(ret.data(), data(), new_size); return ret;} //! Return sub-array starts from "index" and has "count" or less bytes PIByteArray getRange(size_t index, size_t count) const { return PIDeque::getRange(index, count); } //! Convert data to Base 64 and return this byte array PIByteArray & convertToBase64(); //! Convert data from Base 64 and return this byte array PIByteArray & convertFromBase64(); //! Return converted to Base 64 data PIByteArray toBase64() const; PIByteArray & compressRLE(uchar threshold = 192); PIByteArray & decompressRLE(uchar threshold = 192); PIByteArray compressedRLE(uchar threshold = 192) {PIByteArray ba(*this); ba.compressRLE(threshold); return ba;} PIByteArray decompressedRLE(uchar threshold = 192) {PIByteArray ba(*this); ba.decompressRLE(threshold); return ba;} PIString toString(int base = 16) const; //! Returns a hex encoded copy of the byte array. //! The hex encoding uses the numbers 0-9 and the letters a-f. PIString toHex() const; //! Add to the end data "data" with size "size" PIByteArray & append(const void * data_, int size_) {uint ps = size(); enlarge(size_); memcpy(data(ps), data_, size_); return *this;} //! Add to the end byte array "data" PIByteArray & append(const PIByteArray & data_) {uint ps = size(); enlarge(data_.size_s()); memcpy(data(ps), data_.data(), data_.size()); return *this;} //! Add to the end "t" PIByteArray & append(uchar t) {push_back(t); return *this;} //! Returns 8-bit checksum //! sum all bytes, if inverse - add 1, inverse //! Pseudocode: //! sum += at(i); //! return ~(sum + 1) uchar checksumPlain8(bool inverse = true) const; //! Returns 32-bit checksum //! sum all bytes multiplyed by index+1, if inverse - add 1, inverse //! Pseudocode: //! sum += at(i) * (i + 1); //! return ~(sum + 1) uint checksumPlain32(bool inverse = true) const; //! Returns hash uint hash() const; void operator =(const PIDeque & d) {resize(d.size()); memcpy(data(), d.data(), d.size());} PIByteArray & operator =(const PIByteArray & o) {if (this == &o) return *this; clear(); append(o); return *this;} PIByteArray & operator =(PIByteArray && o) {swap(o); return *this;} static PIByteArray fromUserInput(PIString str); static PIByteArray fromHex(PIString str); //! Return converted from Base 64 data static PIByteArray fromBase64(const PIByteArray & base64); static PIByteArray fromBase64(const PIString & base64); class StreamRef { public: StreamRef(PIByteArray & s): ba(s) {} operator PIByteArray&() {return ba;} private: PIByteArray & ba; }; }; //! \relatesalso PIByteArray @brief Byte arrays compare operator inline bool operator <(const PIByteArray & v0, const PIByteArray & v1) { if (v0.size() == v1.size()) { if (v0.isEmpty()) return false; return memcmp(v0.data(), v1.data(), v0.size()) < 0; } return v0.size() < v1.size(); } //! \relatesalso PIByteArray @brief Byte arrays compare operator inline bool operator >(const PIByteArray & v0, const PIByteArray & v1) { if (v0.size() == v1.size()) { if (v0.isEmpty()) return false; return memcmp(v0.data(), v1.data(), v0.size()) > 0; } return v0.size() > v1.size(); } //! \relatesalso PIByteArray @brief Byte arrays compare operator inline bool operator ==(const PIByteArray & v0, const PIByteArray & v1) { if (v0.size() == v1.size()) { if (v0.isEmpty()) return true; return memcmp(v0.data(), v1.data(), v0.size()) == 0; } return false; } //! \relatesalso PIByteArray @brief Byte arrays compare operator inline bool operator !=(const PIByteArray & v0, const PIByteArray & v1) { if (v0.size() == v1.size()) { if (v0.isEmpty()) return false; return memcmp(v0.data(), v1.data(), v0.size()) != 0; } return true; } #ifdef PIP_STD_IOSTREAM //! \relatesalso PIByteArray @brief Output to std::ostream operator inline std::ostream & operator <<(std::ostream & s, const PIByteArray & ba); #endif //! \relatesalso PIByteArray @brief Output to PICout operator PIP_EXPORT PICout operator <<(PICout s, const PIByteArray & ba); // store operators for basic types //! \relatesalso PIByteArray @brief Store operator inline PIByteArray & operator <<(PIByteArray & s, const bool v) {s.push_back(v); return s;} //! \relatesalso PIByteArray @brief Store operator inline PIByteArray & operator <<(PIByteArray & s, const char v) {s.push_back(v); return s;} //! \relatesalso PIByteArray @brief Store operator inline PIByteArray & operator <<(PIByteArray & s, const uchar v) {s.push_back(v); return s;} //! \relatesalso PIByteArray @brief Store operator for any trivial copyable type template::value, int>::type = 0> inline PIByteArray::StreamRef operator <<(PIByteArray & s, const T & v) { int os = s.size_s(); s.enlarge(sizeof(v)); memcpy(s.data(os), &v, sizeof(v)); return s; } //! \relatesalso PIByteArray @brief Store operator, see \ref PIByteArray_sec1 for details PIP_EXPORT PIByteArray & operator <<(PIByteArray & s, const PIByteArray & v); //! \relatesalso PIByteArray @brief Store operator, see \ref PIByteArray_sec1 for details inline PIByteArray & operator <<(PIByteArray & s, const PIByteArray::RawData & v) { int os = s.size_s(); if (v.s > 0) { s.enlarge(v.s); memcpy(s.data(os), v.d, v.s); } return s; } //! \relatesalso PIByteArray @brief Store operator for PIVector of any trivial copyable type template::value, int>::type = 0, typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector & v) { s << int(v.size_s()); int os = s.size_s(); if (v.size_s() > 0) { s.enlarge(v.size_s()*sizeof(T)); memcpy(s.data(os), v.data(), v.size_s()*sizeof(T)); } return s; } template::value, int>::type = 0, typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector & v) { s << int(v.size_s()); for (uint i = 0; i < v.size(); ++i) s << v[i]; return s; } //! \relatesalso PIByteArray @brief Store operator for PIDeque of any trivial copyable type template::value, int>::type = 0, typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIDeque & v) { s << int(v.size_s()); int os = s.size_s(); if (v.size_s() > 0) { s.enlarge(v.size_s()*sizeof(T)); memcpy(s.data(os), v.data(), v.size_s()*sizeof(T)); } return s; } template::value, int>::type = 0, typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIDeque & v) { s << int(v.size_s()); for (uint i = 0; i < v.size(); ++i) s << v[i]; return s; } //! \relatesalso PIByteArray @brief Store operator for PIVector2D of any trivial copyable type template::value, int>::type = 0, typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D & v) { s << int(v.rows()) << int(v.cols()); int os = s.size_s(); if (v.size_s() > 0) { s.enlarge(v.size_s()*sizeof(T)); memcpy(s.data(os), v.data(), v.size_s()*sizeof(T)); } return s; } template::value, int>::type = 0, typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D & v) { s << int(v.rows()) << int(v.cols()) << v.toPlainVector(); return s; } //! \relatesalso PIByteArray @brief Store operator inline PIByteArray & operator <<(PIByteArray & s, const PIBitArray & v) {s << v.size_ << v.data_; return s;} //! \relatesalso PIPair @brief Store operator template inline PIByteArray & operator <<(PIByteArray & s, const PIPair & v) {s << v.first << v.second; return s;} // restore operators for basic types //! \relatesalso PIByteArray @brief Restore operator inline PIByteArray & operator >>(PIByteArray & s, bool & v) {assert(s.size() >= 1u); v = s.take_front(); return s;} //! \relatesalso PIByteArray @brief Restore operator inline PIByteArray & operator >>(PIByteArray & s, char & v) {assert(s.size() >= 1u); v = s.take_front(); return s;} //! \relatesalso PIByteArray @brief Restore operator inline PIByteArray & operator >>(PIByteArray & s, uchar & v) {assert(s.size() >= 1u); v = s.take_front(); return s;} //! \relatesalso PIByteArray @brief Restore operator for any trivial copyable type template::value, int>::type = 0> inline PIByteArray::StreamRef operator >>(PIByteArray & s, T & v) { if (s.size() < sizeof(v)) { printf("error with %s\n", _TYPENAME_(T)); assert(s.size() >= sizeof(v)); } memcpy((void*)(&v), s.data(), sizeof(v)); s.remove(0, sizeof(v)); return s; } //! \relatesalso PIByteArray @brief Restore operator, see \ref PIByteArray_sec1 for details PIP_EXPORT PIByteArray & operator >>(PIByteArray & s, PIByteArray & v); //! \relatesalso PIByteArray @brief Restore operator, see \ref PIByteArray_sec1 for details inline PIByteArray & operator >>(PIByteArray & s, PIByteArray::RawData v) { if (s.size_s() < v.s) { printf("error with RawData %d < %d\n", (int)s.size_s(), v.s); assert(s.size_s() >= v.s); } if (v.s > 0) { memcpy((void*)(v.d), s.data(), v.s); s.remove(0, v.s); } return s; } //! \relatesalso PIByteArray @brief Restore operator for PIVector of any trivial copyable type template::value, int>::type = 0, typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector & v) { if (s.size_s() < 4) { printf("error with PIVector<%s>\n", _TYPENAME_(T)); assert(s.size_s() >= 4); } int sz; s >> sz; v._resizeRaw(sz); if (sz > 0) { memcpy(v.data(), s.data(), sz*sizeof(T)); s.remove(0, sz*sizeof(T)); } return s; } template::value, int>::type = 0, typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector & v) { if (s.size_s() < 4) { printf("error with PIVector<%s>\n", _TYPENAME_(T)); assert(s.size_s() >= 4); } int sz; s >> sz; v.resize(sz); for (int i = 0; i < sz; ++i) s >> v[i]; return s; } //! \relatesalso PIByteArray @brief Restore operator for PIDeque of any trivial copyable type template::value, int>::type = 0, typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIDeque & v) { if (s.size_s() < 4) { printf("error with PIDeque<%s>\n", _TYPENAME_(T)); assert(s.size_s() >= 4); } int sz; s >> sz; v._resizeRaw(sz); if (sz > 0) { memcpy(v.data(), s.data(), sz*sizeof(T)); s.remove(0, sz*sizeof(T)); } return s; } template::value, int>::type = 0, typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIDeque & v) { if (s.size_s() < 4) { printf("error with PIDeque<%s>\n", _TYPENAME_(T)); assert(s.size_s() >= 4); } int sz; s >> sz; v.resize(sz); for (int i = 0; i < sz; ++i) s >> v[i]; return s; } //! \relatesalso PIByteArray @brief Restore operator for PIVector2D of any trivial copyable type template::value, int>::type = 0, typename std::enable_if< std::is_same() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector2D & v) { if (s.size_s() < 8) { printf("error with PIVecto2Dr<%s>\n", _TYPENAME_(T)); assert(s.size_s() >= 8); } int r, c; s >> r >> c; v._resizeRaw(r, c); int sz = r*c; if (sz > 0) { memcpy(v.data(), s.data(), sz*sizeof(T)); s.remove(0, sz*sizeof(T)); } return s; } template::value, int>::type = 0, typename std::enable_if() << std::declval()), PIByteArray::StreamRef>::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector2D & v) { if (s.size_s() < 8) { printf("error with PIVecto2Dr<%s>\n", _TYPENAME_(T)); assert(s.size_s() >= 8); } int r,c; PIVector tmp; s >> r >> c >> tmp; v = PIVector2D(r, c, tmp); return s; } //! \relatesalso PIByteArray @brief Restore operator inline PIByteArray & operator >>(PIByteArray & s, PIBitArray & v) {assert(s.size_s() >= 8); s >> v.size_ >> v.data_; return s;} //! \relatesalso PIPair @brief Restore operator template inline PIByteArray & operator >>(PIByteArray & s, PIPair & v) {s >> v.first >> v.second; return s;} // store operators for complex types //! \relatesalso PIByteArray @brief Store operator for PIVector of any compound type template::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector & v) { s << int(v.size_s()); for (uint i = 0; i < v.size(); ++i) s << v[i]; return s; } //! \relatesalso PIByteArray @brief Store operator for PIDeque of any compound type template::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIDeque & v) { s << int(v.size_s()); for (uint i = 0; i < v.size(); ++i) s << v[i]; return s; } //! \relatesalso PIByteArray @brief Store operator for PIVector2D of any compound type template::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const PIVector2D & v) { s << int(v.rows()) << int(v.cols()) << v.toPlainVector(); return s; } // restore operators for complex types //! \relatesalso PIByteArray @brief Restore operator for PIVector of any compound type template::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector & v) { if (s.size_s() < 4) { printf("error with PIVector<%s>\n", _TYPENAME_(T)); assert(s.size_s() >= 4); } int sz; s >> sz; v.resize(sz); for (int i = 0; i < sz; ++i) s >> v[i]; return s; } //! \relatesalso PIByteArray @brief Restore operator for PIDeque of any compound type template::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIDeque & v) { if (s.size_s() < 4) { printf("error with PIDeque<%s>\n", _TYPENAME_(T)); assert(s.size_s() >= 4); } int sz; s >> sz; v.resize(sz); for (int i = 0; i < sz; ++i) s >> v[i]; return s; } //! \relatesalso PIByteArray @brief Restore operator for PIVector2D of any compound type template::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, PIVector2D & v) { if (s.size_s() < 8) { printf("error with PIVecto2Dr<%s>\n", _TYPENAME_(T)); assert(s.size_s() >= 8); } int r,c; PIVector tmp; s >> r >> c >> tmp; v = PIVector2D(r, c, tmp); return s; } // other types template inline PIByteArray & operator <<(PIByteArray & s, const PIMap & v) { s << int(v.pim_index.size_s()); for (uint i = 0; i < v.size(); ++i) s << int(v.pim_index[i].index) << v.pim_index[i].key; s << v.pim_content; return s; } template inline PIByteArray & operator >>(PIByteArray & s, PIMap & v) { if (s.size_s() < 4) { printf("error with PIMap<%s, %s>\n", _TYPENAME_(Key), _TYPENAME_(T)); assert(s.size_s() >= 4); } int sz; s >> sz; v.pim_index.resize(sz); int ind = 0; for (int i = 0; i < sz; ++i) { s >> ind >> v.pim_index[i].key; v.pim_index[i].index = ind; } s >> v.pim_content; if (v.pim_content.size_s() != v.pim_index.size_s()) { piCout << "Warning: loaded invalid PIMap, clear"; v.clear(); } return s; } template::value, int>::type = 0> inline PIByteArray & operator <<(PIByteArray & s, const T & ) { static_assert(std::is_trivially_copyable::value, "[PIByteArray] Error: using undeclared operator << for complex type!"); return s; } template::value, int>::type = 0> inline PIByteArray & operator >>(PIByteArray & s, T & ) { static_assert(std::is_trivially_copyable::value, "[PIByteArray] Error: using undeclared operator >> for complex type!"); return s; } template<> inline uint piHash(const PIByteArray & ba) {return ba.hash();} template<> inline void piSwap(PIByteArray & f, PIByteArray & s) {f.swap(s);} template PIByteArray piSerialize(const T & value) { PIByteArray ret; ret << value; return ret; } template T piDeserialize(const PIByteArray & data) { T ret; if (!data.isEmpty()) { PIByteArray ba(data); ba >> ret; } return ret; } #undef _TYPENAME_ #endif // PIBYTEARRAY_H