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 "mms_encode_string.h"
17
18 #include "mms_charset.h"
19 #include "sms_service_manager_client.h"
20 #include "telephony_log_wrapper.h"
21 #include "utils/mms_charset.h"
22
23 namespace OHOS {
24 namespace Telephony {
MmsEncodeString()25 MmsEncodeString::MmsEncodeString() : charset_(0), strEncodeString_("") {}
26
MmsEncodeString(const MmsEncodeString & obj)27 MmsEncodeString::MmsEncodeString(const MmsEncodeString &obj)
28 {
29 this->charset_ = obj.charset_;
30 this->valLength_ = obj.valLength_;
31 this->strEncodeString_ = obj.strEncodeString_;
32 }
33
~MmsEncodeString()34 MmsEncodeString::~MmsEncodeString() {}
35
36 /**
37 * @brief DecodeEncodeString
38 * OMA-TS-MMS_CONF-V1_3-20110913-A section:7.3.19 Encoded-String-Value
39 * Encoded-string-value = Text-string | Value-length Char-set Text-string
40 * End-of-string = <Octet 0>
41 * @param decodeBuffer
42 * @return true
43 * @return false
44 */
DecodeEncodeString(MmsDecodeBuffer & decodeBuffer)45 bool MmsEncodeString::DecodeEncodeString(MmsDecodeBuffer &decodeBuffer)
46 {
47 uint8_t oneByte = 0;
48 const uint8_t maxHasCharsetNum = 30;
49 strEncodeString_.clear();
50 if (!decodeBuffer.PeekOneByte(oneByte)) {
51 TELEPHONY_LOGE("Decode encodeString PeekOneByte fail.");
52 return false;
53 }
54
55 if (oneByte == 0) {
56 strEncodeString_.clear();
57 decodeBuffer.IncreasePointer(1);
58 TELEPHONY_LOGE("Decode encodeString DecodeEncodeString fail.");
59 return false;
60 }
61
62 if (oneByte < maxHasCharsetNum) {
63 if (!decodeBuffer.DecodeValueLength(valLength_)) {
64 TELEPHONY_LOGE("Decode encodeString DecodeValueLength fail.");
65 return false;
66 }
67 uint64_t charset = 0;
68 if (!decodeBuffer.DecodeInteger(charset)) {
69 TELEPHONY_LOGE("Decode encodeString DecodeInteger fail.");
70 return false;
71 }
72 charset_ = static_cast<uint32_t>(charset);
73 }
74
75 uint32_t len = 0;
76 if (!decodeBuffer.DecodeText(strEncodeString_, len)) {
77 TELEPHONY_LOGE("Decode encodeString DecodeText fail.");
78 return false;
79 }
80 valLength_ = len;
81 return true;
82 }
83
84 /**
85 * @brief EncodeEncodeString
86 * OMA-TS-MMS_CONF-V1_3-20110913-A section:7.3.19 Encoded-String-Value
87 * Encoded-string-value = Text-string | Value-length Char-set Text-string
88 * End-of-string = <Octet 0>
89 * @param encodeBuffer
90 * @return true
91 * @return false
92 */
EncodeEncodeString(MmsEncodeBuffer & encodeBuffer)93 bool MmsEncodeString::EncodeEncodeString(MmsEncodeBuffer &encodeBuffer)
94 {
95 MmsEncodeBuffer tempBuffer;
96 if (charset_ != 0) {
97 if ((charset_ & 0xFF00) == 0) {
98 tempBuffer.EncodeShortInteger(charset_);
99 } else {
100 tempBuffer.EncodeInteger(charset_);
101 }
102 if (!tempBuffer.EncodeText(strEncodeString_)) {
103 TELEPHONY_LOGE("EncodeString EncodeText fail.");
104 return false;
105 }
106 if (!encodeBuffer.EncodeValueLength(tempBuffer.GetCurPosition())) {
107 TELEPHONY_LOGE("EncodeString EncodeValueLength fail.");
108 return false;
109 }
110 if (!encodeBuffer.WriteBuffer(tempBuffer)) {
111 TELEPHONY_LOGE("EncodeString WriteBuffer fail.");
112 return false;
113 }
114 return true;
115 }
116 if (!encodeBuffer.EncodeText(strEncodeString_)) {
117 TELEPHONY_LOGE("EncodeString EncodeText fail.");
118 return false;
119 }
120 return true;
121 }
122
GetEncodeString(std::string & encodeString)123 bool MmsEncodeString::GetEncodeString(std::string &encodeString)
124 {
125 auto client = DelayedSingleton<SmsServiceManagerClient>::GetInstance();
126 bool ret = client->GetEncodeStringFunc(encodeString, charset_, valLength_, strEncodeString_);
127 return ret;
128 }
129
SetEncodeString(uint32_t charset,const std::string & encodeString)130 bool MmsEncodeString::SetEncodeString(uint32_t charset, const std::string &encodeString)
131 {
132 valLength_ = encodeString.length();
133 strEncodeString_ = encodeString;
134
135 if (charset == 0) {
136 charset = CHARSET_UTF8;
137 }
138 charset_ = charset;
139 return true;
140 }
141
SetAddressString(MmsAddress & addrsss)142 bool MmsEncodeString::SetAddressString(MmsAddress &addrsss)
143 {
144 std::string enString = addrsss.GetAddressString();
145 MmsCharSets chartSets = addrsss.GetAddressCharset();
146 return SetEncodeString(static_cast<uint32_t>(chartSets), enString);
147 }
148 } // namespace Telephony
149 } // namespace OHOS
150