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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
Copyright (C) 2019-2025 Selwin van Dijk
This file is part of signalbackup-tools.
signalbackup-tools 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 3 of the License, or
(at your option) any later version.
signalbackup-tools 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with signalbackup-tools. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SHAREDPREFFRAME_H_
#define SHAREDPREFFRAME_H_
#include <string_view>
#include "../common_bytes.h"
#include "../backupframe/backupframe.h"
class SharedPrefFrame : public BackupFrame
{
enum FIELD
{
INVALID = 0,
FILE = 1, // string
KEY = 2, // string
VALUE = 3, // string
BOOLEANVALUE = 4, // bool
STRINGSETVALUE = 5, // string (repeated)
ISSTRINGSETVALUE = 6 // bool
};
static Registrar s_registrar;
public:
inline explicit SharedPrefFrame(uint64_t count = 0);
inline SharedPrefFrame(unsigned char const *bytes, size_t length, uint64_t count = 0);
inline virtual ~SharedPrefFrame() override = default;
inline virtual SharedPrefFrame *clone() const override;
inline virtual SharedPrefFrame *move_clone() override;
inline static BackupFrame *create(unsigned char const *bytes, size_t length, uint64_t count = 0);
inline virtual void printInfo() const override;
inline virtual FRAMETYPE frameType() const override;
inline std::pair<unsigned char *, uint64_t> getData() const override;
inline virtual bool validate(uint64_t) const override;
inline std::string getHumanData() const override;
inline unsigned int getField(std::string_view const &str) const;
inline std::string key() const;
inline std::vector<std::string> value() const;
inline std::string valueType() const;
private:
inline uint64_t dataSize() const override;
};
inline SharedPrefFrame::SharedPrefFrame(uint64_t count)
:
BackupFrame(count)
{}
inline SharedPrefFrame::SharedPrefFrame(unsigned char const *bytes, size_t length, uint64_t count)
:
BackupFrame(bytes, length, count)
{}
inline SharedPrefFrame *SharedPrefFrame::clone() const
{
return new SharedPrefFrame(*this);
}
inline SharedPrefFrame *SharedPrefFrame::move_clone()
{
return new SharedPrefFrame(std::move(*this));
}
inline BackupFrame *SharedPrefFrame::create(unsigned char const *bytes, size_t length, uint64_t count)
{
return new SharedPrefFrame(bytes, length, count);
}
inline void SharedPrefFrame::printInfo() const // virtual
{
Logger::message("Frame number: ", d_count);
Logger::message(" Size: ", d_constructedsize);
Logger::message(" Type: SHAREDPREFERENCEFRAME");
for (auto const &p : d_framedata)
{
if (std::get<0>(p) == FIELD::FILE)
Logger::message(" - (file : \"", bepaald::bytesToString(std::get<1>(p), std::get<2>(p)), "\" (", std::get<2>(p), " bytes)");
else if (std::get<0>(p) == FIELD::KEY)
Logger::message(" - (key : \"", bepaald::bytesToString(std::get<1>(p), std::get<2>(p)), "\" (", std::get<2>(p), " bytes)");
else if (std::get<0>(p) == FIELD::VALUE)
Logger::message(" - (value : \"", bepaald::bytesToString(std::get<1>(p), std::get<2>(p)), "\" (", std::get<2>(p), " bytes)");
else if (std::get<0>(p) == FIELD::BOOLEANVALUE)
Logger::message(" - (booleanvalue : \"", std::boolalpha, (bytesToUint64(std::get<1>(p), std::get<2>(p)) ? true : false), "\")");
else if (std::get<0>(p) == FIELD::STRINGSETVALUE)
Logger::message(" - (stringsetvalue : \"", bepaald::bytesToString(std::get<1>(p), std::get<2>(p)), "\" (", std::get<2>(p), " bytes)");
else if (std::get<0>(p) == FIELD::ISSTRINGSETVALUE)
Logger::message(" - (isstringsetvalue : \"", std::boolalpha, (bytesToUint64(std::get<1>(p), std::get<2>(p)) ? true : false), "\")");
}
}
inline BackupFrame::FRAMETYPE SharedPrefFrame::frameType() const // virtual override
{
return FRAMETYPE::SHAREDPREFERENCE;
}
inline uint64_t SharedPrefFrame::dataSize() const
{
uint64_t size = 0;
for (auto const &fd : d_framedata)
{
switch (std::get<0>(fd))
{
case FIELD::FILE:
case FIELD::KEY:
case FIELD::VALUE:
case FIELD::STRINGSETVALUE:
{
uint64_t stringsize = std::get<2>(fd);
size += varIntSize(stringsize);
size += stringsize + 1; // +1 for fieldtype + wiretype
break;
}
case FIELD::BOOLEANVALUE:
case FIELD::ISSTRINGSETVALUE:
{
uint64_t val = bytesToInt64(std::get<1>(fd), std::get<2>(fd));
size += varIntSize(val);
size += 1; // for fieldtype + wiretype
break;
}
}
}
// for size of this entire frame.
size += varIntSize(size);
return ++size;
}
inline std::pair<unsigned char *, uint64_t> SharedPrefFrame::getData() const
{
uint64_t size = dataSize();
unsigned char *data = new unsigned char[size];
uint64_t datapos = 0;
datapos += setFieldAndWire(FRAMETYPE::SHAREDPREFERENCE, WIRETYPE::LENGTHDELIM, data + datapos);
datapos += setFrameSize(size, data + datapos);
for (auto const &fd : d_framedata)
{
switch (std::get<0>(fd))
{
case FIELD::FILE:
case FIELD::KEY:
case FIELD::VALUE:
case FIELD::STRINGSETVALUE:
{
datapos += putLengthDelimType(fd, data + datapos);
break;
}
case FIELD::BOOLEANVALUE:
case FIELD::ISSTRINGSETVALUE:
{
datapos += putVarIntType(fd, data + datapos);
break;
}
}
}
return {data, size};
}
// not sure about the requirements, but at least _a_ field should be set
// also a key needs a value, and a value needs a key
inline bool SharedPrefFrame::validate(uint64_t) const
{
if (d_framedata.empty())
return false;
int foundfile = 0;
int foundkey = 0;
int foundvalue = 0;
bool isstringsetvalue = false;
for (auto const &p : d_framedata)
{
if (std::get<0>(p) != FIELD::FILE &&
std::get<0>(p) != FIELD::KEY &&
std::get<0>(p) != FIELD::VALUE &&
std::get<0>(p) != FIELD::BOOLEANVALUE &&
std::get<0>(p) != FIELD::STRINGSETVALUE &&
std::get<0>(p) != FIELD::ISSTRINGSETVALUE)
return false;
if (std::get<0>(p) == FIELD::FILE)
++foundfile;
if (std::get<0>(p) == FIELD::KEY)
++foundkey;
if (std::get<0>(p) == FIELD::VALUE ||
std::get<0>(p) == FIELD::BOOLEANVALUE ||
std::get<0>(p) == FIELD::STRINGSETVALUE ||
std::get<0>(p) == FIELD::ISSTRINGSETVALUE)
++foundvalue;
if (std::get<0>(p) == FIELD::ISSTRINGSETVALUE)
isstringsetvalue = (bytesToUint64(std::get<1>(p), std::get<2>(p)) ? true : false);
}
return (foundfile + foundkey + foundvalue) > 0 &&
(isstringsetvalue ? (foundkey <= foundvalue) : (foundkey == foundvalue));
}
inline std::string SharedPrefFrame::getHumanData() const
{
std::string data;
for (auto const &p : d_framedata)
{
if (std::get<0>(p) == FIELD::FILE)
data += "FILE:string:" + bepaald::bytesToString(std::get<1>(p), std::get<2>(p)) + "\n";
else if (std::get<0>(p) == FIELD::KEY)
data += "KEY:string:" + bepaald::bytesToString(std::get<1>(p), std::get<2>(p)) + "\n";
else if (std::get<0>(p) == FIELD::VALUE)
data += "VALUE:string:" + bepaald::bytesToString(std::get<1>(p), std::get<2>(p)) + "\n";
else if (std::get<0>(p) == FIELD::BOOLEANVALUE)
data += "BOOLEANVALUE:bool:" + (bytesToInt64(std::get<1>(p), std::get<2>(p)) ? "true"s : "false"s) + "\n";
else if (std::get<0>(p) == FIELD::STRINGSETVALUE)
data += "STRINGSETVALUE:string:" + bepaald::bytesToString(std::get<1>(p), std::get<2>(p)) + "\n";
else if (std::get<0>(p) == FIELD::ISSTRINGSETVALUE)
data += "ISSTRINGSETVALUE:bool:" + (bytesToInt64(std::get<1>(p), std::get<2>(p)) ? "true"s : "false"s) + "\n";
}
return data;
}
inline unsigned int SharedPrefFrame::getField(std::string_view const &str) const
{
if (str == "FILE")
return FIELD::FILE;
if (str == "KEY")
return FIELD::KEY;
if (str == "VALUE")
return FIELD::VALUE;
if (str == "BOOLEANVALUE")
return FIELD::BOOLEANVALUE;
if (str == "STRINGSETVALUE")
return FIELD::STRINGSETVALUE;
if (str == "ISSTRINGSETVALUE")
return FIELD::ISSTRINGSETVALUE;
return FIELD::INVALID;
}
inline std::string SharedPrefFrame::key() const
{
for (auto const &p : d_framedata)
if (std::get<0>(p) == FIELD::KEY)
return bepaald::bytesToString(std::get<1>(p), std::get<2>(p));
return std::string();
}
inline std::vector<std::string> SharedPrefFrame::value() const
{
std::vector<std::string> ret;
for (auto const &p : d_framedata)
{
if (std::get<0>(p) == FIELD::VALUE) // string
{
ret.push_back(bepaald::bytesToString(std::get<1>(p), std::get<2>(p)));
break;
}
if (std::get<0>(p) == FIELD::BOOLEANVALUE)
{
ret.emplace_back((bytesToInt64(std::get<1>(p), std::get<2>(p)) ? "true"s : "false"s));
break;
}
if (std::get<0>(p) == FIELD::STRINGSETVALUE)
ret.emplace_back(bepaald::bytesToString(std::get<1>(p), std::get<2>(p)));
}
return ret;
}
inline std::string SharedPrefFrame::valueType() const
{
for (auto const &p : d_framedata)
{
if (std::get<0>(p) == FIELD::VALUE)
return "STRING";
if (std::get<0>(p) == FIELD::BOOLEANVALUE)
return "BOOL";
if (std::get<0>(p) == FIELD::STRINGSETVALUE)
return "STRINGSET";
if (std::get<0>(p) == FIELD::ISSTRINGSETVALUE)
if (bytesToInt64(std::get<1>(p), std::get<2>(p)) == true)
return "STRINGSET";
}
Logger::warning("Currently unsupported value type requested from SharedPrefrenceFrame");
return std::string();
}
#endif
|