Make TlStorerUnsafe buffer unsigned.

GitOrigin-RevId: 8bb19c10e4edd8b015d3a2be0449426c9c07f7df
This commit is contained in:
levlam 2018-06-25 02:58:43 +03:00
parent 019c0bb376
commit 046ad3b752
12 changed files with 49 additions and 55 deletions

View File

@ -193,7 +193,7 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection
string encrypted_data_str(encrypted_data_size_with_pad, 0); string encrypted_data_str(encrypted_data_size_with_pad, 0);
MutableSlice encrypted_data = encrypted_data_str; MutableSlice encrypted_data = encrypted_data_str;
as<int32>(encrypted_data.begin() + 20) = data.get_id(); as<int32>(encrypted_data.begin() + 20) = data.get_id();
tl_store_unsafe(data, encrypted_data.begin() + 20 + 4); tl_store_unsafe(data, encrypted_data.ubegin() + 20 + 4);
sha1(Slice(encrypted_data.ubegin() + 20, data_size), encrypted_data.ubegin()); sha1(Slice(encrypted_data.ubegin() + 20, data_size), encrypted_data.ubegin());
Random::secure_bytes(encrypted_data.ubegin() + encrypted_data_size, Random::secure_bytes(encrypted_data.ubegin() + encrypted_data_size,
encrypted_data_size_with_pad - encrypted_data_size); encrypted_data_size_with_pad - encrypted_data_size);

View File

@ -29,10 +29,9 @@ class PacketStorer
} }
size_t store(uint8 *ptr) const override { size_t store(uint8 *ptr) const override {
char *start = reinterpret_cast<char *>(ptr); TlStorerUnsafe storer(ptr);
TlStorerUnsafe storer(start);
this->do_store(storer); this->do_store(storer);
return storer.get_buf() - start; return static_cast<size_t>(storer.get_buf() - ptr);
} }
using Impl::Impl; using Impl::Impl;

View File

@ -86,11 +86,10 @@ class TLObjectStorer : public Storer {
return size_; return size_;
} }
size_t store(uint8 *ptr) const override { size_t store(uint8 *ptr) const override {
char *p = reinterpret_cast<char *>(ptr); TlStorerUnsafe storer(ptr);
TlStorerUnsafe storer(p);
storer.store_binary(object_.get_id()); storer.store_binary(object_.get_id());
object_.store(storer); object_.store(storer);
return storer.get_buf() - p; return static_cast<size_t>(storer.get_buf() - ptr);
} }
}; };

View File

@ -4856,7 +4856,7 @@ BufferSlice MessagesManager::get_dialog_database_value(const Dialog *d) {
BufferSlice value_buffer{storer_calc_length.get_length()}; BufferSlice value_buffer{storer_calc_length.get_length()};
auto value = value_buffer.as_slice(); auto value = value_buffer.as_slice();
LogEventStorerUnsafe storer_unsafe(value.begin()); LogEventStorerUnsafe storer_unsafe(value.ubegin());
store(*d, storer_unsafe); store(*d, storer_unsafe);
return value_buffer; return value_buffer;
} }

View File

@ -2407,7 +2407,7 @@ string StickersManager::get_sticker_set_database_value(const StickerSet *s, bool
LOG(DEBUG) << "Sticker set " << s->id << " serialized size is " << value.size(); LOG(DEBUG) << "Sticker set " << s->id << " serialized size is " << value.size();
LogEventStorerUnsafe storer_unsafe(value.begin()); LogEventStorerUnsafe storer_unsafe(value.ubegin());
store_sticker_set(s, with_stickers, storer_unsafe); store_sticker_set(s, with_stickers, storer_unsafe);
return value.str(); return value.str();

View File

@ -297,7 +297,7 @@ Status fix_file_remote_location_key_bug(SqliteDb &db) {
kv.init_with_connection(db.clone(), "files").ensure(); kv.init_with_connection(db.clone(), "files").ensure();
auto ptr = StackAllocator::alloc(4); auto ptr = StackAllocator::alloc(4);
MutableSlice prefix = ptr.as_slice(); MutableSlice prefix = ptr.as_slice();
TlStorerUnsafe(prefix.begin()).store_int(OLD_KEY_MAGIC); TlStorerUnsafe(prefix.ubegin()).store_int(OLD_KEY_MAGIC);
kv.get_by_prefix(prefix, [&](Slice key, Slice value) { kv.get_by_prefix(prefix, [&](Slice key, Slice value) {
CHECK(TlParser(key).fetch_int() == OLD_KEY_MAGIC); CHECK(TlParser(key).fetch_int() == OLD_KEY_MAGIC);
auto remote_str = PSTRING() << key.substr(4, 4) << Slice("\0\0\0\0") << key.substr(8); auto remote_str = PSTRING() << key.substr(4, 4) << Slice("\0\0\0\0") << key.substr(8);

View File

@ -1293,7 +1293,7 @@ string as_key(const T &object) {
BufferSlice key_buffer{calc_length.get_length()}; BufferSlice key_buffer{calc_length.get_length()};
auto key = key_buffer.as_slice(); auto key = key_buffer.as_slice();
TlStorerUnsafe storer(key.begin()); TlStorerUnsafe storer(key.ubegin());
storer.store_int(T::KEY_MAGIC); storer.store_int(T::KEY_MAGIC);
object.as_key().store(storer); object.as_key().store(storer);
return key.str(); return key.str();

View File

@ -168,15 +168,14 @@ class StorerImpl : public Storer {
td::store(event_, storer); td::store(event_, storer);
return storer.get_length(); return storer.get_length();
} }
size_t store(uint8 *ptr_x) const override { size_t store(uint8 *ptr) const override {
char *ptr = reinterpret_cast<char *>(ptr_x);
WithContext<TlStorerUnsafe, Global *> storer(ptr); WithContext<TlStorerUnsafe, Global *> storer(ptr);
storer.set_context(G()); storer.set_context(G());
storer.store_int(T::version()); storer.store_int(T::version());
td::store(magic(event_), storer); td::store(magic(event_), storer);
td::store(event_, storer); td::store(event_, storer);
return storer.get_buf() - ptr; return static_cast<size_t>(storer.get_buf() - ptr);
} }
private: private:
@ -227,7 +226,7 @@ class LogEventStorerCalcLength : public WithContext<TlStorerCalcLength, Global *
class LogEventStorerUnsafe : public WithContext<TlStorerUnsafe, Global *> { class LogEventStorerUnsafe : public WithContext<TlStorerUnsafe, Global *> {
public: public:
explicit LogEventStorerUnsafe(char *buf) : WithContext<TlStorerUnsafe, Global *>(buf) { explicit LogEventStorerUnsafe(unsigned char *buf) : WithContext<TlStorerUnsafe, Global *>(buf) {
store_int(static_cast<int32>(Version::Next) - 1); store_int(static_cast<int32>(Version::Next) - 1);
set_context(G()); set_context(G());
} }
@ -262,15 +261,14 @@ class LogEventStorerImpl : public Storer {
td::store(event_, storer); td::store(event_, storer);
return storer.get_length(); return storer.get_length();
} }
size_t store(uint8 *ptr_x) const override { size_t store(uint8 *ptr) const override {
char *ptr = reinterpret_cast<char *>(ptr_x);
LogEventStorerUnsafe storer(ptr); LogEventStorerUnsafe storer(ptr);
td::store(event_, storer); td::store(event_, storer);
#ifdef TD_DEBUG #ifdef TD_DEBUG
T check_result; T check_result;
log_event_parse(check_result, Slice(ptr, storer.get_buf())).ensure(); log_event_parse(check_result, Slice(ptr, storer.get_buf())).ensure();
#endif #endif
return storer.get_buf() - ptr; return static_cast<size_t>(storer.get_buf() - ptr);
} }
private: private:
@ -284,7 +282,7 @@ BufferSlice log_event_store(const T &data) {
BufferSlice value_buffer{storer_calc_length.get_length()}; BufferSlice value_buffer{storer_calc_length.get_length()};
LogEventStorerUnsafe storer_unsafe(value_buffer.as_slice().begin()); LogEventStorerUnsafe storer_unsafe(value_buffer.as_slice().ubegin());
store(data, storer_unsafe); store(data, storer_unsafe);
#ifdef TD_DEBUG #ifdef TD_DEBUG

View File

@ -57,11 +57,10 @@ class BinlogKeyValue : public KeyValueSyncInterface {
store(storer); store(storer);
return storer.get_length(); return storer.get_length();
} }
size_t store(uint8 *ptr_x) const override { size_t store(uint8 *ptr) const override {
auto ptr = reinterpret_cast<char *>(ptr_x);
TlStorerUnsafe storer(ptr); TlStorerUnsafe storer(ptr);
store(storer); store(storer);
return storer.get_buf() - ptr; return static_cast<size_t>(storer.get_buf() - ptr);
} }
}; };

View File

@ -95,17 +95,17 @@ inline StringBuilder &operator<<(StringBuilder &sb, const BinlogEvent &event) {
inline BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) { inline BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) {
auto raw_event = BufferSlice{storer.size() + MIN_EVENT_SIZE}; auto raw_event = BufferSlice{storer.size() + MIN_EVENT_SIZE};
TlStorerUnsafe tl_storer(raw_event.as_slice().begin()); TlStorerUnsafe tl_storer(raw_event.as_slice().ubegin());
tl_storer.store_int(narrow_cast<int32>(raw_event.size())); tl_storer.store_int(narrow_cast<int32>(raw_event.size()));
tl_storer.store_long(id); tl_storer.store_long(id);
tl_storer.store_int(type); tl_storer.store_int(type);
tl_storer.store_int(flags); tl_storer.store_int(flags);
tl_storer.store_long(0); tl_storer.store_long(0);
CHECK(tl_storer.get_buf() == raw_event.as_slice().begin() + EVENT_HEADER_SIZE); CHECK(tl_storer.get_buf() == raw_event.as_slice().ubegin() + EVENT_HEADER_SIZE);
tl_storer.store_storer(storer); tl_storer.store_storer(storer);
CHECK(tl_storer.get_buf() == raw_event.as_slice().end() - EVENT_TAIL_SIZE); CHECK(tl_storer.get_buf() == raw_event.as_slice().uend() - EVENT_TAIL_SIZE);
tl_storer.store_int(::td::crc32(raw_event.as_slice().truncate(raw_event.size() - EVENT_TAIL_SIZE))); tl_storer.store_int(::td::crc32(raw_event.as_slice().truncate(raw_event.size() - EVENT_TAIL_SIZE)));
return raw_event; return raw_event;

View File

@ -187,12 +187,12 @@ string serialize(const T &object) {
if (!is_aligned_pointer<4>(key.data())) { if (!is_aligned_pointer<4>(key.data())) {
auto ptr = StackAllocator::alloc(length); auto ptr = StackAllocator::alloc(length);
MutableSlice data = ptr.as_slice(); MutableSlice data = ptr.as_slice();
TlStorerUnsafe storer(data.begin()); TlStorerUnsafe storer(data.ubegin());
store(object, storer); store(object, storer);
key.assign(data.begin(), data.size()); key.assign(data.begin(), data.size());
} else { } else {
MutableSlice data = key; MutableSlice data = key;
TlStorerUnsafe storer(data.begin()); TlStorerUnsafe storer(data.ubegin());
store(object, storer); store(object, storer);
} }
return key; return key;

View File

@ -17,11 +17,11 @@
namespace td { namespace td {
class TlStorerUnsafe { class TlStorerUnsafe {
char *buf; unsigned char *buf_;
public: public:
explicit TlStorerUnsafe(char *buf) : buf(buf) { explicit TlStorerUnsafe(unsigned char *buf) : buf_(buf) {
CHECK(is_aligned_pointer<4>(buf)); CHECK(is_aligned_pointer<4>(buf_));
} }
TlStorerUnsafe(const TlStorerUnsafe &other) = delete; TlStorerUnsafe(const TlStorerUnsafe &other) = delete;
@ -29,13 +29,13 @@ class TlStorerUnsafe {
template <class T> template <class T>
void store_binary(const T &x) { void store_binary(const T &x) {
std::memcpy(buf, reinterpret_cast<const unsigned char *>(&x), sizeof(T)); std::memcpy(buf_, reinterpret_cast<const unsigned char *>(&x), sizeof(T));
buf += sizeof(T); buf_ += sizeof(T);
} }
void store_int(int32 x) { void store_int(int32 x) {
*reinterpret_cast<int32 *>(buf) = x; *reinterpret_cast<int32 *>(buf_) = x;
buf += sizeof(int32); buf_ += sizeof(int32);
} }
void store_long(int64 x) { void store_long(int64 x) {
@ -43,45 +43,45 @@ class TlStorerUnsafe {
} }
void store_slice(Slice slice) { void store_slice(Slice slice) {
std::memcpy(buf, slice.begin(), slice.size()); std::memcpy(buf_, slice.begin(), slice.size());
buf += slice.size(); buf_ += slice.size();
} }
void store_storer(const Storer &storer) { void store_storer(const Storer &storer) {
size_t size = storer.store(reinterpret_cast<unsigned char *>(buf)); size_t size = storer.store(buf_);
buf += size; buf_ += size;
} }
template <class T> template <class T>
void store_string(const T &str) { void store_string(const T &str) {
size_t len = str.size(); size_t len = str.size();
if (len < 254) { if (len < 254) {
*buf++ = static_cast<char>(len); *buf_++ = static_cast<unsigned char>(len);
len++; len++;
} else if (len < (1 << 24)) { } else if (len < (1 << 24)) {
*buf++ = static_cast<char>(static_cast<unsigned char>(254)); *buf_++ = static_cast<unsigned char>(254);
*buf++ = static_cast<char>(len & 255); *buf_++ = static_cast<unsigned char>(len & 255);
*buf++ = static_cast<char>((len >> 8) & 255); *buf_++ = static_cast<unsigned char>((len >> 8) & 255);
*buf++ = static_cast<char>(len >> 16); *buf_++ = static_cast<unsigned char>(len >> 16);
} else { } else {
LOG(FATAL) << "String size " << len << " is too big to be stored"; LOG(FATAL) << "String size " << len << " is too big to be stored";
} }
std::memcpy(buf, str.data(), str.size()); std::memcpy(buf_, str.data(), str.size());
buf += str.size(); buf_ += str.size();
switch (len & 3) { switch (len & 3) {
case 1: case 1:
*buf++ = '\0'; *buf_++ = 0;
// fallthrough // fallthrough
case 2: case 2:
*buf++ = '\0'; *buf_++ = 0;
// fallthrough // fallthrough
case 3: case 3:
*buf++ = '\0'; *buf_++ = 0;
} }
} }
char *get_buf() const { unsigned char *get_buf() const {
return buf; return buf_;
} }
}; };
@ -272,12 +272,11 @@ size_t tl_calc_length(const T &data) {
return storer_calc_length.get_length(); return storer_calc_length.get_length();
} }
template <class T, class CharT> template <class T>
size_t tl_store_unsafe(const T &data, CharT *dst) { size_t tl_store_unsafe(const T &data, unsigned char *dst) {
char *start = reinterpret_cast<char *>(dst); TlStorerUnsafe storer_unsafe(dst);
TlStorerUnsafe storer_unsafe(start);
data.store(storer_unsafe); data.store(storer_unsafe);
return storer_unsafe.get_buf() - start; return static_cast<size_t>(storer_unsafe.get_buf() - dst);
} }
} // namespace td } // namespace td