1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "stream_depacketizer.h"
17
18 #include "common_inner.h"
19 #include "i_stream.h"
20
21 namespace Communication {
22 namespace SoftBus {
DepacketizeHeader(const char * header)23 void StreamDepacketizer::DepacketizeHeader(const char *header)
24 {
25 if (streamType_ == COMMON_VIDEO_STREAM || streamType_ == COMMON_AUDIO_STREAM) {
26 const char *ptr = header;
27 header_.Depacketize(ptr);
28
29 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_DBG,
30 "streamPktHeader version = %d, subVersion = %d, extFlag = %d, streamType = %d, marker = %d, flag = %d"
31 "streamId = %d (%x), timestamp = %u (%x), dataLen = %u (%x), seqNum = %d (%x), subSeqNum = %d (%x)",
32 header_.GetVersion(), header_.GetSubVersion(), header_.GetExtFlag(), header_.GetStreamType(),
33 header_.GetMarker(), header_.GetFlag(), header_.GetStreamId(), header_.GetStreamId(),
34 header_.GetTimestamp(), header_.GetTimestamp(), header_.GetDataLen(), header_.GetDataLen(),
35 header_.GetSeqNum(), header_.GetSeqNum(), header_.GetSubSeqNum(), header_.GetSubSeqNum());
36 }
37 }
38
DepacketizeBuffer(char * buffer,uint32_t bufferSize)39 void StreamDepacketizer::DepacketizeBuffer(char *buffer, uint32_t bufferSize)
40 {
41 char *ptr = buffer;
42 uint32_t tlvTotalLen = 0;
43 if (header_.GetExtFlag() != 0) {
44 tlvs_.Depacketize(ptr, bufferSize);
45 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO,
46 "TLV version: %d, num = %d, extLen = %zd, checksum = %u", tlvs_.GetVersion(), tlvs_.GetTlvNums(),
47 tlvs_.GetExtLen(), tlvs_.GetCheckSum());
48
49 tlvTotalLen = tlvs_.GetCheckSum() + sizeof(tlvs_.GetCheckSum());
50 ptr += tlvTotalLen;
51 }
52
53 dataLength_ = static_cast<int>(header_.GetDataLen() - tlvTotalLen);
54 if (dataLength_ <= 0 || dataLength_ > MAX_STREAM_LEN) {
55 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
56 "DepacketizeBuffer error, header_dataLen = %u, tlvTotalLen = %u", header_.GetDataLen(), tlvTotalLen);
57 return;
58 }
59
60 int remain = static_cast<int>(bufferSize - (ptr - buffer));
61 if (remain < dataLength_) {
62 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
63 "Data out of bounds, remain = %d, dataLength_ = %d", remain, dataLength_);
64 return;
65 }
66
67 data_ = std::make_unique<char[]>(dataLength_);
68 auto ret = memcpy_s(data_.get(), dataLength_, ptr, dataLength_);
69 if (ret != 0) {
70 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Failed to memcpy data_, ret:%d", ret);
71 dataLength_ = -1;
72 }
73 }
74 } // namespace SoftBus
75 } // namespace Communication
76