| 6 |
| * Copyright Nikolai Kudashov, 2015. |
| 7 | */ |
| 8 |
| 9 | #include <memory.h> |
| 10 | #include <arpa/inet.h> |
| 11 | #include "MTProtoScheme.h" |
| 12 | #include "ApiScheme.h" |
| 13 | #include "FileLog.h" |
| 14 | #include "ByteArray.h" |
| 15 | #include "NativeByteBuffer.h" |
| 16 | #include "BuffersStorage.h" |
| 17 | #include "ConnectionsManager.h" |
| 18 |
| 19 | TLObject *TLClassStore::TLdeserialize(NativeByteBuffer *stream, uint32_t bytes, uint32_t constructor, bool &error) { |
| 20 | TLObject *object = nullptr; |
| 21 | switch (constructor) { |
| 22 | case TL_msgs_ack::constructor: |
| 23 | object = new TL_msgs_ack(); |
| 24 | break; |
| 25 | case TL_msg_container::constructor: |
| 26 | object = new TL_msg_container(); |
| 27 | break; |
| 28 | case TL_pong::constructor: |
| 29 | object = new TL_pong(); |
| 30 | break; |
| 31 | case TL_new_session_created::constructor: |
| 32 | object = new TL_new_session_created(); |
| 33 | break; |
| 34 | case TL_rpc_result::constructor: |
| 35 | object = new TL_rpc_result(); |
| 36 | ((TL_rpc_result *) object)->readParamsEx(stream, bytes, error); |
| 37 | return object; |
| 38 | case TL_bad_msg_notification::constructor: |
| 39 | object = new TL_bad_msg_notification(); |
| 40 | break; |
| 41 | case TL_bad_server_salt::constructor: |
| 42 | object = new TL_bad_server_salt(); |
| 43 | break; |
| 44 | case TL_msg_detailed_info::constructor: |
| 45 | object = new TL_msg_detailed_info(); |
| 46 | break; |
| 47 | case TL_msg_new_detailed_info::constructor: |
| 48 | object = new TL_msg_new_detailed_info(); |
| 49 | break; |
| 50 | case TL_gzip_packed::constructor: |
| 51 | object = new TL_gzip_packed(); |
| 52 | break; |
| 53 | case TL_error::constructor: |
| 54 | object = new TL_error(); |
| 55 | break; |
| 56 | case TL_rpc_error::constructor: |
| 57 | object = new TL_rpc_error(); |
| 58 | break; |
| 59 | case TL_rpc_req_error::constructor: |
| 60 | object = new TL_rpc_req_error(); |
| 61 | break; |
| 62 | case TL_future_salts::constructor: |
| 63 | object = new TL_future_salts(); |
| 64 | break; |
| 65 | case TL_destroy_session_ok::constructor: |
| 66 | object = new TL_destroy_session_ok(); |
| 67 | break; |
| 68 | case TL_destroy_session_none::constructor: |
| 69 | object = new TL_destroy_session_none(); |
| 70 | break; |
| 71 | case TL_updatesTooLong::constructor: |
| 72 | object = new TL_updatesTooLong(); |
| 73 | break; |
| 74 | default: |
| 75 | return nullptr; |
| 76 | } |
| 77 | object->readParams(stream, error); |
| 78 | return object; |
| 79 | } |
| 80 |
| 81 | TL_api_request::~TL_api_request() { |
| 82 | if (request != nullptr) { |
| 83 | request->reuse(); |
| 84 | request = nullptr; |
| 85 | } |
| 86 | } |
| 87 |
| 88 | bool TL_api_request::isNeedLayer() { |
| 89 | return true; |
| 90 | } |
| 91 |
| 92 | TLObject *TL_api_request::deserializeResponse(NativeByteBuffer *stream, uint32_t bytes, bool &error) { |
| 93 | TL_api_response *result = new TL_api_response(); |
| 94 | result->readParamsEx(stream, bytes, error); |
| 95 | return result; |
| 96 | } |
| 97 |
| 98 | void TL_api_request::serializeToStream(NativeByteBuffer *stream) { |
| 99 | request->rewind(); |
| 100 | stream->writeBytes(request); |
| 101 | } |
| 102 |
| 103 | void TL_api_response::readParamsEx(NativeByteBuffer *stream, uint32_t bytes, bool &error) { |
| 104 | response = std::unique_ptr<NativeByteBuffer>(new NativeByteBuffer(stream->bytes() + stream->position() - 4, bytes)); |
| 105 | stream->skip((uint32_t) (bytes - 4)); |
| 106 | } |
| 107 |
| 108 | TL_future_salt *TL_future_salt::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 109 | if (TL_future_salt::constructor != constructor) { |
| 110 | error = true; |
| 111 | DEBUG_E("can't parse magic %x in TL_future_salt", constructor); |
| 112 | return nullptr; |
| 113 | } |
| 114 | TL_future_salt *result = new TL_future_salt(); |
| 115 | result->readParams(stream, error); |
| 116 | return result; |
| 117 | } |
| 118 |
| 119 | void TL_future_salt::readParams(NativeByteBuffer *stream, bool &error) { |
| 120 | valid_since = stream->readInt32(&error); |
| 121 | valid_until = stream->readInt32(&error); |
| 122 | salt = stream->readInt64(&error); |
| 123 | } |
| 124 |
| 125 | TL_msgs_state_info *TL_msgs_state_info::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 126 | if (TL_msgs_state_info::constructor != constructor) { |
| 127 | error = true; |
| 128 | DEBUG_E("can't parse magic %x in TL_msgs_state_info", constructor); |
| 129 | return nullptr; |
| 130 | } |
| 131 | TL_msgs_state_info *result = new TL_msgs_state_info(); |
| 132 | result->readParams(stream, error); |
| 133 | return result; |
| 134 | } |
| 135 |
| 136 | void TL_msgs_state_info::readParams(NativeByteBuffer *stream, bool &error) { |
| 137 | req_msg_id = stream->readInt64(&error); |
| 138 | info = std::unique_ptr<ByteArray>(stream->readByteArray(&error)); |
| 139 | } |
| 140 |
| 141 | void TL_msgs_state_info::serializeToStream(NativeByteBuffer *stream) { |
| 142 | stream->writeInt32(constructor); |
| 143 | stream->writeInt64(req_msg_id); |
| 144 | stream->writeByteArray(info.get()); |
| 145 | } |
| 146 |
| 147 | Server_DH_Params *Server_DH_Params::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 148 | Server_DH_Params *result = nullptr; |
| 149 | switch (constructor) { |
| 150 | case 0x79cb045d: |
| 151 | result = new TL_server_DH_params_fail(); |
| 152 | break; |
| 153 | case 0xd0e8075c: |
| 154 | result = new TL_server_DH_params_ok(); |
| 155 | break; |
| 156 | default: |
| 157 | error = true; |
| 158 | DEBUG_E("can't parse magic %x in Server_DH_Params", constructor); |
| 159 | return nullptr; |
| 160 | } |
| 161 | result->readParams(stream, error); |
| 162 | return result; |
| 163 | } |
| 164 |
| 165 | void TL_server_DH_params_fail::readParams(NativeByteBuffer *stream, bool &error) { |
| 166 | nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 167 | server_nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 168 | new_nonce_hash = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 169 | } |
| 170 |
| 171 | void TL_server_DH_params_ok::readParams(NativeByteBuffer *stream, bool &error) { |
| 172 | nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 173 | server_nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 174 | encrypted_answer = std::unique_ptr<ByteArray>(stream->readByteArray(&error)); |
| 175 | } |
| 176 |
| 177 | TL_resPQ *TL_resPQ::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 178 | if (TL_resPQ::constructor != constructor) { |
| 179 | error = true; |
| 180 | DEBUG_E("can't parse magic %x in TL_resPQ", constructor); |
| 181 | return nullptr; |
| 182 | } |
| 183 | TL_resPQ *result = new TL_resPQ(); |
| 184 | result->readParams(stream, error); |
| 185 | return result; |
| 186 | } |
| 187 |
| 188 | void TL_resPQ::readParams(NativeByteBuffer *stream, bool &error) { |
| 189 | nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 190 | server_nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 191 | pq = std::unique_ptr<ByteArray>(stream->readByteArray(&error)); |
| 192 | uint32_t magic = stream->readUint32(&error); |
| 193 | if (magic != 0x1cb5c415) { |
| 194 | error = true; |
| 195 | DEBUG_E("wrong Vector magic, got %x", magic); |
| 196 | return; |
| 197 | } |
| 198 | uint32_t count = stream->readUint32(&error); |
| 199 | if (count * sizeof(int64_t) + stream->position() > stream->limit()) { |
| 200 | error = true; |
| 201 | return; |
| 202 | } |
| 203 | for (uint32_t a = 0; a < count; a++) { |
| 204 | server_public_key_fingerprints.push_back(stream->readInt64(&error)); |
| 205 | } |
| 206 | } |
| 207 |
| 208 | void TL_p_q_inner_data::serializeToStream(NativeByteBuffer *stream) { |
| 209 | stream->writeInt32(constructor); |
| 210 | stream->writeByteArray(pq.get()); |
| 211 | stream->writeByteArray(p.get()); |
| 212 | stream->writeByteArray(q.get()); |
| 213 | stream->writeBytes(nonce.get()); |
| 214 | stream->writeBytes(server_nonce.get()); |
| 215 | stream->writeBytes(new_nonce.get()); |
| 216 | } |
| 217 |
| 218 | TL_pong *TL_pong::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 219 | if (TL_pong::constructor != constructor) { |
| 220 | error = true; |
| 221 | DEBUG_E("can't parse magic %x in TL_pong", constructor); |
| 222 | return nullptr; |
| 223 | } |
| 224 | TL_pong *result = new TL_pong(); |
| 225 | result->readParams(stream, error); |
| 226 | return result; |
| 227 | } |
| 228 |
| 229 | void TL_pong::readParams(NativeByteBuffer *stream, bool &error) { |
| 230 | msg_id = stream->readInt64(&error); |
| 231 | ping_id = stream->readInt64(&error); |
| 232 | } |
| 233 |
| 234 | TL_future_salts *TL_future_salts::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 235 | if (TL_future_salts::constructor != constructor) { |
| 236 | error = true; |
| 237 | DEBUG_E("can't parse magic %x in TL_future_salts", constructor); |
| 238 | return nullptr; |
| 239 | } |
| 240 | TL_future_salts *result = new TL_future_salts(); |
| 241 | result->readParams(stream, error); |
| 242 | return result; |
| 243 | } |
| 244 |
| 245 | void TL_future_salts::readParams(NativeByteBuffer *stream, bool &error) { |
| 246 | req_msg_id = stream->readInt64(&error); |
| 247 | now = stream->readInt32(&error); |
| 248 | uint32_t count = stream->readUint32(&error); |
| 249 | for (uint32_t a = 0; a < count; a++) { |
| 250 | TL_future_salt *object = new TL_future_salt(); |
| 251 | object->readParams(stream, error); |
| 252 | if (error) { |
| 253 | return; |
| 254 | } |
| 255 | salts.push_back(std::unique_ptr<TL_future_salt>(object)); |
| 256 | } |
| 257 | } |
| 258 |
| 259 | RpcDropAnswer *RpcDropAnswer::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 260 | RpcDropAnswer *result = nullptr; |
| 261 | switch (constructor) { |
| 262 | case 0x5e2ad36e: |
| 263 | result = new TL_rpc_answer_unknown(); |
| 264 | break; |
| 265 | case 0xa43ad8b7: |
| 266 | result = new TL_rpc_answer_dropped(); |
| 267 | break; |
| 268 | case 0xcd78e586: |
| 269 | result = new TL_rpc_answer_dropped_running(); |
| 270 | break; |
| 271 | default: |
| 272 | error = true; |
| 273 | DEBUG_E("can't parse magic %x in RpcDropAnswer", constructor); |
| 274 | return nullptr; |
| 275 | } |
| 276 | result->readParams(stream, error); |
| 277 | return result; |
| 278 | } |
| 279 |
| 280 | void TL_rpc_answer_unknown::serializeToStream(NativeByteBuffer *stream) { |
| 281 | stream->writeInt32(constructor); |
| 282 | } |
| 283 |
| 284 | void TL_rpc_answer_dropped::readParams(NativeByteBuffer *stream, bool &error) { |
| 285 | msg_id = stream->readInt64(&error); |
| 286 | seq_no = stream->readInt32(&error); |
| 287 | bytes = stream->readInt32(&error); |
| 288 | } |
| 289 |
| 290 | void TL_rpc_answer_dropped_running::serializeToStream(NativeByteBuffer *stream) { |
| 291 | stream->writeInt32(constructor); |
| 292 | } |
| 293 |
| 294 | Set_client_DH_params_answer *Set_client_DH_params_answer::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 295 | Set_client_DH_params_answer *result = nullptr; |
| 296 | switch (constructor) { |
| 297 | case 0x46dc1fb9: |
| 298 | result = new TL_dh_gen_retry(); |
| 299 | break; |
| 300 | case 0xa69dae02: |
| 301 | result = new TL_dh_gen_fail(); |
| 302 | break; |
| 303 | case 0x3bcbf734: |
| 304 | result = new TL_dh_gen_ok(); |
| 305 | break; |
| 306 | default: |
| 307 | error = true; |
| 308 | DEBUG_E("can't parse magic %x in Set_client_DH_params_answer", constructor); |
| 309 | return nullptr; |
| 310 | } |
| 311 | result->readParams(stream, error); |
| 312 | return result; |
| 313 | } |
| 314 |
| 315 | void TL_dh_gen_retry::readParams(NativeByteBuffer *stream, bool &error) { |
| 316 | nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 317 | server_nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 318 | new_nonce_hash2 = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 319 | } |
| 320 |
| 321 | void TL_dh_gen_fail::readParams(NativeByteBuffer *stream, bool &error) { |
| 322 | nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 323 | server_nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 324 | new_nonce_hash3 = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 325 | } |
| 326 |
| 327 | void TL_dh_gen_ok::readParams(NativeByteBuffer *stream, bool &error) { |
| 328 | nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 329 | server_nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 330 | new_nonce_hash1 = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 331 | } |
| 332 |
| 333 | BadMsgNotification *BadMsgNotification::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 334 | BadMsgNotification *result = nullptr; |
| 335 | switch (constructor) { |
| 336 | case 0xa7eff811: |
| 337 | result = new TL_bad_msg_notification(); |
| 338 | break; |
| 339 | case 0xedab447b: |
| 340 | result = new TL_bad_server_salt(); |
| 341 | break; |
| 342 | default: |
| 343 | error = true; |
| 344 | DEBUG_E("can't parse magic %x in BadMsgNotification", constructor); |
| 345 | return nullptr; |
| 346 | } |
| 347 | result->readParams(stream, error); |
| 348 | return result; |
| 349 | } |
| 350 |
| 351 | void TL_bad_msg_notification::readParams(NativeByteBuffer *stream, bool &error) { |
| 352 | bad_msg_id = stream->readInt64(&error); |
| 353 | bad_msg_seqno = stream->readInt32(&error); |
| 354 | error_code = stream->readInt32(&error); |
| 355 | } |
| 356 |
| 357 | void TL_bad_server_salt::readParams(NativeByteBuffer *stream, bool &error) { |
| 358 | bad_msg_id = stream->readInt64(&error); |
| 359 | bad_msg_seqno = stream->readInt32(&error); |
| 360 | error_code = stream->readInt32(&error); |
| 361 | new_server_salt = stream->readInt64(&error); |
| 362 | } |
| 363 |
| 364 | TL_msgs_state_req *TL_msgs_state_req::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 365 | if (TL_msgs_state_req::constructor != constructor) { |
| 366 | error = true; |
| 367 | DEBUG_E("can't parse magic %x in TL_msgs_state_req", constructor); |
| 368 | return nullptr; |
| 369 | } |
| 370 | TL_msgs_state_req *result = new TL_msgs_state_req(); |
| 371 | result->readParams(stream, error); |
| 372 | return result; |
| 373 | } |
| 374 |
| 375 | void TL_msgs_state_req::readParams(NativeByteBuffer *stream, bool &error) { |
| 376 | uint32_t magic = stream->readUint32(&error); |
| 377 | if (magic != 0x1cb5c415) { |
| 378 | error = true; |
| 379 | DEBUG_E("wrong Vector magic, got %x", magic); |
| 380 | return; |
| 381 | } |
| 382 | uint32_t count = stream->readUint32(&error); |
| 383 | if (count * sizeof(int64_t) + stream->position() > stream->limit()) { |
| 384 | error = true; |
| 385 | return; |
| 386 | } |
| 387 | for (uint32_t a = 0; a < count; a++) { |
| 388 | msg_ids.push_back(stream->readInt64(&error)); |
| 389 | } |
| 390 | } |
| 391 |
| 392 | void TL_msgs_state_req::serializeToStream(NativeByteBuffer *stream) { |
| 393 | stream->writeInt32(constructor); |
| 394 | stream->writeInt32(0x1cb5c415); |
| 395 | uint32_t count = (uint32_t) msg_ids.size(); |
| 396 | stream->writeInt32(count); |
| 397 | for (uint32_t a = 0; a < count; a++) { |
| 398 | stream->writeInt64(msg_ids[a]); |
| 399 | } |
| 400 | } |
| 401 |
| 402 | MsgDetailedInfo *MsgDetailedInfo::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 403 | MsgDetailedInfo *result = nullptr; |
| 404 | switch (constructor) { |
| 405 | case 0x809db6df: |
| 406 | result = new TL_msg_new_detailed_info(); |
| 407 | break; |
| 408 | case 0x276d3ec6: |
| 409 | result = new TL_msg_detailed_info(); |
| 410 | break; |
| 411 | default: |
| 412 | error = true; |
| 413 | DEBUG_E("can't parse magic %x in MsgDetailedInfo", constructor); |
| 414 | return nullptr; |
| 415 | } |
| 416 | result->readParams(stream, error); |
| 417 | return result; |
| 418 | } |
| 419 |
| 420 | void TL_msg_new_detailed_info::readParams(NativeByteBuffer *stream, bool &error) { |
| 421 | answer_msg_id = stream->readInt64(&error); |
| 422 | bytes = stream->readInt32(&error); |
| 423 | status = stream->readInt32(&error); |
| 424 | } |
| 425 |
| 426 | void TL_msg_detailed_info::readParams(NativeByteBuffer *stream, bool &error) { |
| 427 | msg_id = stream->readInt64(&error); |
| 428 | answer_msg_id = stream->readInt64(&error); |
| 429 | bytes = stream->readInt32(&error); |
| 430 | status = stream->readInt32(&error); |
| 431 | } |
| 432 |
| 433 | TL_msg_copy *TL_msg_copy::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 434 | if (TL_msg_copy::constructor != constructor) { |
| 435 | error = true; |
| 436 | DEBUG_E("can't parse magic %x in TL_msg_copy", constructor); |
| 437 | return nullptr; |
| 438 | } |
| 439 | TL_msg_copy *result = new TL_msg_copy(); |
| 440 | result->readParams(stream, error); |
| 441 | return result; |
| 442 | } |
| 443 |
| 444 | void TL_msg_copy::readParams(NativeByteBuffer *stream, bool &error) { |
| 445 | orig_message = std::unique_ptr<TL_message>(TL_message::TLdeserialize(stream, stream->readUint32(&error), error)); |
| 446 | } |
| 447 |
| 448 | void TL_msg_copy::serializeToStream(NativeByteBuffer *stream) { |
| 449 | stream->writeInt32(constructor); |
| 450 | orig_message->serializeToStream(stream); |
| 451 | } |
| 452 |
| 453 | TL_msgs_all_info *TL_msgs_all_info::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 454 | if (TL_msgs_all_info::constructor != constructor) { |
| 455 | error = true; |
| 456 | DEBUG_E("can't parse magic %x in TL_msgs_all_info", constructor); |
| 457 | return nullptr; |
| 458 | } |
| 459 | TL_msgs_all_info *result = new TL_msgs_all_info(); |
| 460 | result->readParams(stream, error); |
| 461 | return result; |
| 462 | } |
| 463 |
| 464 | void TL_msgs_all_info::readParams(NativeByteBuffer *stream, bool &error) { |
| 465 | uint32_t magic = stream->readUint32(&error); |
| 466 | if (magic != 0x1cb5c415) { |
| 467 | error = true; |
| 468 | DEBUG_E("wrong Vector magic, got %x", magic); |
| 469 | return; |
| 470 | } |
| 471 | uint32_t count = stream->readUint32(&error); |
| 472 | if (count * sizeof(int64_t) + stream->position() > stream->limit()) { |
| 473 | error = true; |
| 474 | return; |
| 475 | } |
| 476 | for (uint32_t a = 0; a < count; a++) { |
| 477 | msg_ids.push_back(stream->readInt64(&error)); |
| 478 | } |
| 479 | info = std::unique_ptr<ByteArray>(stream->readByteArray(&error)); |
| 480 | } |
| 481 |
| 482 | void TL_rpc_result::readParamsEx(NativeByteBuffer *stream, uint32_t bytes, bool &error) { |
| 483 | req_msg_id = stream->readInt64(&error); |
| 484 | TLObject *object = ConnectionsManager::getInstance().TLdeserialize(ConnectionsManager::getInstance().getRequestWithMessageId(req_msg_id), bytes - 12, stream); |
| 485 | if (object != nullptr) { |
| 486 | result = std::unique_ptr<TLObject>(object); |
| 487 | } else { |
| 488 | error = true; |
| 489 | } |
| 490 | } |
| 491 |
| 492 | void TL_new_session_created::readParams(NativeByteBuffer *stream, bool &error) { |
| 493 | first_msg_id = stream->readInt64(&error); |
| 494 | unique_id = stream->readInt64(&error); |
| 495 | server_salt = stream->readInt64(&error); |
| 496 | } |
| 497 |
| 498 | DestroySessionRes *DestroySessionRes::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 499 | DestroySessionRes *result = nullptr; |
| 500 | switch (constructor) { |
| 501 | case 0xe22045fc: |
| 502 | result = new TL_destroy_session_ok(); |
| 503 | break; |
| 504 | case 0x62d350c9: |
| 505 | result = new TL_destroy_session_none(); |
| 506 | break; |
| 507 | default: |
| 508 | error = true; |
| 509 | DEBUG_E("can't parse magic %x in DestroySessionRes", constructor); |
| 510 | return nullptr; |
| 511 | } |
| 512 | result->readParams(stream, error); |
| 513 | return result; |
| 514 | } |
| 515 |
| 516 | void TL_destroy_session_ok::readParams(NativeByteBuffer *stream, bool &error) { |
| 517 | session_id = stream->readInt64(&error); |
| 518 | } |
| 519 |
| 520 | void TL_destroy_session_none::readParams(NativeByteBuffer *stream, bool &error) { |
| 521 | session_id = stream->readInt64(&error); |
| 522 | } |
| 523 |
| 524 | TL_msgs_ack *TL_msgs_ack::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 525 | if (TL_msgs_ack::constructor != constructor) { |
| 526 | error = true; |
| 527 | DEBUG_E("can't parse magic %x in TL_msgs_ack", constructor); |
| 528 | return nullptr; |
| 529 | } |
| 530 | TL_msgs_ack *result = new TL_msgs_ack(); |
| 531 | result->readParams(stream, error); |
| 532 | return result; |
| 533 | } |
| 534 |
| 535 | void TL_msgs_ack::readParams(NativeByteBuffer *stream, bool &error) { |
| 536 | uint32_t magic = stream->readUint32(&error); |
| 537 | if (magic != 0x1cb5c415) { |
| 538 | error = true; |
| 539 | DEBUG_E("wrong Vector magic, got %x", magic); |
| 540 | return; |
| 541 | } |
| 542 | uint32_t count = stream->readUint32(&error); |
| 543 | if (count * sizeof(int64_t) + stream->position() > stream->limit()) { |
| 544 | error = true; |
| 545 | return; |
| 546 | } |
| 547 | for (uint32_t a = 0; a < count; a++) { |
| 548 | msg_ids.push_back(stream->readInt64(&error)); |
| 549 | } |
| 550 | } |
| 551 |
| 552 | void TL_msgs_ack::serializeToStream(NativeByteBuffer *stream) { |
| 553 | stream->writeInt32(constructor); |
| 554 | stream->writeInt32(0x1cb5c415); |
| 555 | uint32_t count = (uint32_t) msg_ids.size(); |
| 556 | stream->writeInt32(count); |
| 557 | for (uint32_t a = 0; a < count; a++) { |
| 558 | stream->writeInt64(msg_ids[a]); |
| 559 | } |
| 560 | } |
| 561 |
| 562 | TL_msg_container *TL_msg_container::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 563 | if (TL_msg_container::constructor != constructor) { |
| 564 | error = true; |
| 565 | DEBUG_E("can't parse magic %x in TL_msg_container", constructor); |
| 566 | return nullptr; |
| 567 | } |
| 568 | TL_msg_container *result = new TL_msg_container(); |
| 569 | result->readParams(stream, error); |
| 570 | return result; |
| 571 | } |
| 572 |
| 573 | void TL_msg_container::readParams(NativeByteBuffer *stream, bool &error) { |
| 574 | uint32_t count = stream->readUint32(&error); |
| 575 | for (uint32_t a = 0; a < count; a++) { |
| 576 | TL_message *object = new TL_message(); |
| 577 | object->readParams(stream, error); |
| 578 | if (error) { |
| 579 | return; |
| 580 | } |
| 581 | messages.push_back(std::unique_ptr<TL_message>(object)); |
| 582 | } |
| 583 | } |
| 584 |
| 585 | void TL_msg_container::serializeToStream(NativeByteBuffer *stream) { |
| 586 | stream->writeInt32(constructor); |
| 587 | uint32_t count = (uint32_t) messages.size(); |
| 588 | stream->writeInt32(count); |
| 589 | for (uint32_t a = 0; a < count; a++) { |
| 590 | messages[a]->serializeToStream(stream); |
| 591 | } |
| 592 | } |
| 593 |
| 594 | TL_message *TL_message::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 595 | if (TL_message::constructor != constructor) { |
| 596 | error = true; |
| 597 | DEBUG_E("can't parse magic %x in TL_message", constructor); |
| 598 | return nullptr; |
| 599 | } |
| 600 | TL_message *result = new TL_message(); |
| 601 | result->readParams(stream, error); |
| 602 | return result; |
| 603 | } |
| 604 |
| 605 | void TL_message::readParams(NativeByteBuffer *stream, bool &error) { |
| 606 | msg_id = stream->readInt64(&error); |
| 607 | seqno = stream->readInt32(&error); |
| 608 | bytes = stream->readInt32(&error); |
| 609 | TLObject *object = ConnectionsManager::getInstance().TLdeserialize(nullptr, (uint32_t) bytes, stream); |
| 610 | if (object == nullptr) { |
| 611 | unparsedBody = std::unique_ptr<NativeByteBuffer>(new NativeByteBuffer(stream->bytes() + stream->position(), (uint32_t) bytes)); |
| 612 | stream->skip((uint32_t) bytes); |
| 613 | } else { |
| 614 | body = std::unique_ptr<TLObject>(object); |
| 615 | } |
| 616 | } |
| 617 |
| 618 | void TL_message::serializeToStream(NativeByteBuffer *stream) { |
| 619 | stream->writeInt64(msg_id); |
| 620 | stream->writeInt32(seqno); |
| 621 | stream->writeInt32(bytes); |
| 622 | if (outgoingBody != nullptr) { |
| 623 | outgoingBody->serializeToStream(stream); |
| 624 | } else { |
| 625 | body->serializeToStream(stream); |
| 626 | } |
| 627 | } |
| 628 |
| 629 | TL_msg_resend_req *TL_msg_resend_req::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 630 | if (TL_msg_resend_req::constructor != constructor) { |
| 631 | error = true; |
| 632 | DEBUG_E("can't parse magic %x in TL_msg_resend_req", constructor); |
| 633 | return nullptr; |
| 634 | } |
| 635 | TL_msg_resend_req *result = new TL_msg_resend_req(); |
| 636 | result->readParams(stream, error); |
| 637 | return result; |
| 638 | } |
| 639 |
| 640 | void TL_msg_resend_req::readParams(NativeByteBuffer *stream, bool &error) { |
| 641 | uint32_t magic = stream->readUint32(&error); |
| 642 | if (magic != 0x1cb5c415) { |
| 643 | error = true; |
| 644 | DEBUG_E("wrong Vector magic, got %x", magic); |
| 645 | return; |
| 646 | } |
| 647 | uint32_t count = stream->readUint32(&error); |
| 648 | if (count * sizeof(int64_t) + stream->position() > stream->limit()) { |
| 649 | error = true; |
| 650 | return; |
| 651 | } |
| 652 | for (uint32_t a = 0; a < count; a++) { |
| 653 | msg_ids.push_back(stream->readInt64(&error)); |
| 654 | } |
| 655 | } |
| 656 |
| 657 | void TL_msg_resend_req::serializeToStream(NativeByteBuffer *stream) { |
| 658 | stream->writeInt32(constructor); |
| 659 | stream->writeInt32(0x1cb5c415); |
| 660 | uint32_t count = (uint32_t) msg_ids.size(); |
| 661 | stream->writeInt32(count); |
| 662 | for (uint32_t a = 0; a < count; a++) { |
| 663 | stream->writeInt64(msg_ids[a]); |
| 664 | } |
| 665 | } |
| 666 |
| 667 | void TL_rpc_error::readParams(NativeByteBuffer *stream, bool &error) { |
| 668 | error_code = stream->readInt32(&error); |
| 669 | error_message = stream->readString(&error); |
| 670 | } |
| 671 |
| 672 | void TL_rpc_req_error::readParams(NativeByteBuffer *stream, bool &error) { |
| 673 | query_id = stream->readInt64(&error); |
| 674 | error_code = stream->readInt32(&error); |
| 675 | error_message = stream->readString(&error); |
| 676 | } |
| 677 |
| 678 | void TL_client_DH_inner_data::serializeToStream(NativeByteBuffer *stream) { |
| 679 | stream->writeInt32(constructor); |
| 680 | stream->writeBytes(nonce.get()); |
| 681 | stream->writeBytes(server_nonce.get()); |
| 682 | stream->writeInt64(retry_id); |
| 683 | stream->writeByteArray(g_b.get()); |
| 684 | } |
| 685 |
| 686 | TL_server_DH_inner_data *TL_server_DH_inner_data::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 687 | if (TL_server_DH_inner_data::constructor != constructor) { |
| 688 | error = true; |
| 689 | DEBUG_E("can't parse magic %x in TL_server_DH_inner_data", constructor); |
| 690 | return nullptr; |
| 691 | } |
| 692 | TL_server_DH_inner_data *result = new TL_server_DH_inner_data(); |
| 693 | result->readParams(stream, error); |
| 694 | return result; |
| 695 | } |
| 696 |
| 697 | void TL_server_DH_inner_data::readParams(NativeByteBuffer *stream, bool &error) { |
| 698 | nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 699 | server_nonce = std::unique_ptr<ByteArray>(stream->readBytes(16, &error)); |
| 700 | g = stream->readUint32(&error); |
| 701 | dh_prime = std::unique_ptr<ByteArray>(stream->readByteArray(&error)); |
| 702 | g_a = std::unique_ptr<ByteArray>(stream->readByteArray(&error)); |
| 703 | server_time = stream->readInt32(&error); |
| 704 | } |
| 705 |
| 706 | void TL_server_DH_inner_data::serializeToStream(NativeByteBuffer *stream) { |
| 707 | stream->writeInt32(constructor); |
| 708 | stream->writeBytes(nonce.get()); |
| 709 | stream->writeBytes(server_nonce.get()); |
| 710 | stream->writeInt32(g); |
| 711 | stream->writeByteArray(dh_prime.get()); |
| 712 | stream->writeByteArray(g_a.get()); |
| 713 | stream->writeInt32(server_time); |
| 714 | } |
| 715 |
| 716 | TLObject *TL_req_pq::deserializeResponse(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 717 | return TL_resPQ::TLdeserialize(stream, constructor, error); |
| 718 | } |
| 719 |
| 720 | void TL_req_pq::serializeToStream(NativeByteBuffer *stream) { |
| 721 | stream->writeInt32(constructor); |
| 722 | stream->writeBytes(nonce.get()); |
| 723 | } |
| 724 |
| 725 | TLObject *TL_req_DH_params::deserializeResponse(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 726 | return Server_DH_Params::TLdeserialize(stream, constructor, error); |
| 727 | } |
| 728 |
| 729 | void TL_req_DH_params::serializeToStream(NativeByteBuffer *stream) { |
| 730 | stream->writeInt32(constructor); |
| 731 | stream->writeBytes(nonce.get()); |
| 732 | stream->writeBytes(server_nonce.get()); |
| 733 | stream->writeByteArray(p.get()); |
| 734 | stream->writeByteArray(q.get()); |
| 735 | stream->writeInt64(public_key_fingerprint); |
| 736 | stream->writeByteArray(encrypted_data.get()); |
| 737 | } |
| 738 |
| 739 | TLObject *TL_set_client_DH_params::deserializeResponse(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 740 | return Set_client_DH_params_answer::TLdeserialize(stream, constructor, error); |
| 741 | } |
| 742 |
| 743 | void TL_set_client_DH_params::serializeToStream(NativeByteBuffer *stream) { |
| 744 | stream->writeInt32(constructor); |
| 745 | stream->writeBytes(nonce.get()); |
| 746 | stream->writeBytes(server_nonce.get()); |
| 747 | stream->writeByteArray(encrypted_data.get()); |
| 748 | } |
| 749 |
| 750 | TLObject *TL_rpc_drop_answer::deserializeResponse(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 751 | return RpcDropAnswer::TLdeserialize(stream, constructor, error); |
| 752 | } |
| 753 |
| 754 | void TL_rpc_drop_answer::serializeToStream(NativeByteBuffer *stream) { |
| 755 | stream->writeInt32(constructor); |
| 756 | stream->writeInt64(req_msg_id); |
| 757 | } |
| 758 |
| 759 | TLObject *TL_get_future_salts::deserializeResponse(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 760 | return TL_future_salts::TLdeserialize(stream, constructor, error); |
| 761 | } |
| 762 |
| 763 | void TL_get_future_salts::serializeToStream(NativeByteBuffer *stream) { |
| 764 | stream->writeInt32(constructor); |
| 765 | stream->writeInt32(num); |
| 766 | } |
| 767 |
| 768 | TLObject *TL_ping::deserializeResponse(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 769 | return TL_pong::TLdeserialize(stream, constructor, error); |
| 770 | } |
| 771 |
| 772 | void TL_ping::serializeToStream(NativeByteBuffer *stream) { |
| 773 | stream->writeInt32(constructor); |
| 774 | stream->writeInt64(ping_id); |
| 775 | } |
| 776 |
| 777 | TLObject *TL_ping_delay_disconnect::deserializeResponse(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 778 | return TL_pong::TLdeserialize(stream, constructor, error); |
| 779 | } |
| 780 |
| 781 | void TL_ping_delay_disconnect::serializeToStream(NativeByteBuffer *stream) { |
| 782 | stream->writeInt32(constructor); |
| 783 | stream->writeInt64(ping_id); |
| 784 | stream->writeInt32(disconnect_delay); |
| 785 | } |
| 786 |
| 787 | TLObject *TL_destroy_session::deserializeResponse(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 788 | return DestroySessionRes::TLdeserialize(stream, constructor, error); |
| 789 | } |
| 790 |
| 791 | void TL_destroy_session::serializeToStream(NativeByteBuffer *stream) { |
| 792 | stream->writeInt32(constructor); |
| 793 | stream->writeInt64(session_id); |
| 794 | } |
| 795 |
| 796 | TL_gzip_packed::~TL_gzip_packed() { |
| 797 | if (packed_data_to_send != nullptr) { |
| 798 | packed_data_to_send->reuse(); |
| 799 | packed_data_to_send = nullptr; |
| 800 | } |
| 801 | } |
| 802 |
| 803 | void TL_gzip_packed::readParams(NativeByteBuffer *stream, bool &error) { |
| 804 | packed_data = std::unique_ptr<NativeByteBuffer>(stream->readByteBuffer(false, &error)); |
| 805 | } |
| 806 |
| 807 | void TL_gzip_packed::serializeToStream(NativeByteBuffer *stream) { |
| 808 | stream->writeInt32(constructor); |
| 809 | stream->writeByteArray(packed_data_to_send); |
| 810 | } |
| 811 |
| 812 | TL_error *TL_error::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 813 | if (TL_error::constructor != constructor) { |
| 814 | error = true; |
| 815 | DEBUG_E("can't parse magic %x in TL_error", constructor); |
| 816 | return nullptr; |
| 817 | } |
| 818 | TL_error *result = new TL_error(); |
| 819 | result->readParams(stream, error); |
| 820 | return result; |
| 821 | } |
| 822 |
| 823 | void TL_error::readParams(NativeByteBuffer *stream, bool &error) { |
| 824 | code = stream->readInt32(&error); |
| 825 | text = stream->readString(&error); |
| 826 | } |
| 827 |
| 828 | void TL_error::serializeToStream(NativeByteBuffer *stream) { |
| 829 | stream->writeInt32(constructor); |
| 830 | stream->writeInt32(code); |
| 831 | stream->writeString(text); |
| 832 | } |
| 833 |
| 834 | void TL_invokeAfterMsg::serializeToStream(NativeByteBuffer *stream) { |
| 835 | stream->writeInt32(constructor); |
| 836 | stream->writeInt64(msg_id); |
| 837 | if (outgoingQuery != nullptr) { |
| 838 | outgoingQuery->serializeToStream(stream); |
| 839 | } else { |
| 840 | query->serializeToStream(stream); |
| 841 | } |
| 842 | } |
| 843 |
| 844 | void invokeWithLayer::serializeToStream(NativeByteBuffer *stream) { |
| 845 | stream->writeInt32(constructor); |
| 846 | stream->writeInt32(layer); |
| 847 | query->serializeToStream(stream); |
| 848 | } |
| 849 |
| 850 | void initConnection::serializeToStream(NativeByteBuffer *stream) { |
| 851 | stream->writeInt32(constructor); |
| 852 | stream->writeInt32(api_id); |
| 853 | stream->writeString(device_model); |
| 854 | stream->writeString(system_version); |
| 855 | stream->writeString(app_version); |
| 856 | stream->writeString(system_lang_code); |
| 857 | stream->writeString(lang_pack); |
| 858 | stream->writeString(lang_code); |
| 859 | query->serializeToStream(stream); |
| 860 | } |
| 861 |
| 862 | void TL_ipPort::readParams(NativeByteBuffer *stream, bool &error) { |
| 863 | struct in_addr ip_addr; |
| 864 | ip_addr.s_addr = htonl(stream->readUint32(&error)); |
| 865 | ipv4 = inet_ntoa(ip_addr); |
| 866 | port = stream->readUint32(&error); |
| 867 | } |
| 868 |
| 869 | TL_help_configSimple *TL_help_configSimple::TLdeserialize(NativeByteBuffer *stream, uint32_t constructor, bool &error) { |
| 870 | if (TL_help_configSimple::constructor != constructor) { |
| 871 | error = true; |
| 872 | DEBUG_E("can't parse magic %x in TL_help_configSimple", constructor); |
| 873 | return nullptr; |
| 874 | } |
| 875 | TL_help_configSimple *result = new TL_help_configSimple(); |
| 876 | result->readParams(stream, error); |
| 877 | return result; |
| 878 | } |
| 879 |
| 880 | void TL_help_configSimple::readParams(NativeByteBuffer *stream, bool &error) { |
| 881 | date = stream->readInt32(&error); |
| 882 | expires = stream->readInt32(&error); |
| 883 | dc_id = stream->readUint32(&error); |
| 884 | int32_t magic = stream->readInt32(&error); |
| 885 | if (magic != 0x1cb5c415) { |
| 886 | error = true; |
| 887 | DEBUG_E("wrong Vector magic, got %x", magic); |
| 888 | return; |
| 889 | } |
| 890 | uint32_t count = stream->readUint32(&error); |
| 891 | for (uint32_t a = 0; a < count; a++) { |
| 892 | TL_ipPort *object = new TL_ipPort(); |
| 893 | object->readParams(stream, error); |
| 894 | if (error) { |
| 895 | return; |
| 896 | } |
| 897 | ip_port_list.push_back(std::unique_ptr<TL_ipPort>(object)); |
| 898 | } |
| 899 | } |
| 900 |