1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
This file is part of Android File Transfer For Linux.
Copyright (C) 2015-2020 Vladimir Menshakov
This library 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 2.1 of the License,
or (at your option) any later version.
This library 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 library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef AFTL_MTP_PTP_OUTPUTSTREAM_H
#define AFTL_MTP_PTP_OUTPUTSTREAM_H
#include <mtp/types.h>
#include <mtp/ByteArray.h>
namespace mtp
{
class OutputStream //! MTP format encoding output stream
{
ByteArray & _data;
public:
OutputStream(ByteArray &data): _data(data)
{ _data.reserve(512); }
ByteArray & GetData()
{ return _data; }
const ByteArray & GetData() const
{ return _data; }
void Write8(u8 value)
{ _data.push_back(value); }
void Write16(u16 value)
{ Write8(value); Write8(value >> 8); }
void Write32(u32 value)
{ Write16(value); Write16(value >> 16); }
void Write64(u32 value)
{ Write32(value); Write32(value >> 16); }
static size_t Utf8Length(const std::string &value)
{
size_t size = 0;
for(char c: value)
{
if ((c & 0x80) == 0 || (c & 0xc0) != 0x80)
++size;
}
return size;
}
void WriteData(const ByteArray & data)
{
_data.reserve(data.size() + _data.size());
for(auto b : data)
Write8(b);
}
void WriteString(const std::string &value)
{
if (value.empty())
{
Write8(0);
return;
}
size_t len = 1 + Utf8Length(value);
if (len > 255)
throw std::runtime_error("string is too big (only 255 chars allowed, including null terminator)");
Write8(len);
for(size_t i = 0, p = 0; i < len && p < value.size(); ++i)
{
u8 c0 = value[p++];
u16 uni;
if (c0 == 0xc0 || c0 == 0xc1 || c0 >= 0xf5)
{
uni = '?';
}
else if (c0 < 0x80)
{
uni = c0;
}
else
{
u8 c1 = value[p++];
if (c0 >= 0xc2 && c0 <= 0xdf)
uni = ((c0 & 0x1f) << 6) | (c1 & 0x3f);
else
{
u8 c2 = value[p++];
if (c0 >= 0xe0 && c0 <= 0xef)
uni = ((c0 & 0x0f) << 12) | ((c1 & 0x3f) << 6) | (c2 & 0x3f);
else
uni = '?';
}
}
Write16(uni);
}
Write16(0);
}
template<typename ArrayType>
void WriteArray(const ArrayType & array)
{
Write32(array.size());
for(const auto & el : array)
(*this) << el;
}
};
inline OutputStream & operator << (OutputStream &stream, char value)
{ stream.Write8(value); return stream; }
inline OutputStream & operator << (OutputStream &stream, u8 value)
{ stream.Write8(value); return stream; }
inline OutputStream & operator << (OutputStream &stream, u16 value)
{ stream.Write16(value); return stream; }
inline OutputStream & operator << (OutputStream &stream, u32 value)
{ stream.Write32(value); return stream; }
inline OutputStream & operator << (OutputStream &stream, u64 value)
{ stream.Write64(value); return stream; }
inline OutputStream & operator << (OutputStream &stream, const std::string &value)
{ stream.WriteString(value); return stream; }
template<typename ElementType>
inline OutputStream & operator << (OutputStream &stream, const std::vector<ElementType> &value)
{ stream.template WriteArray<ElementType>(value); return stream; }
}
#endif
|