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