• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     const uint8_t lengthQuote = 0x1f;
50     strEncodeString_.clear();
51     if (!decodeBuffer.PeekOneByte(oneByte)) {
52         TELEPHONY_LOGE("Decode encodeString PeekOneByte fail.");
53         return false;
54     }
55 
56     if (oneByte == 0) {
57         strEncodeString_.clear();
58         decodeBuffer.IncreasePointer(1);
59         TELEPHONY_LOGE("Decode encodeString DecodeEncodeString fail.");
60         return false;
61     }
62 
63     if (oneByte <= maxHasCharsetNum || oneByte == lengthQuote) {
64         if (!decodeBuffer.DecodeValueLength(valLength_)) {
65             TELEPHONY_LOGE("Decode encodeString DecodeValueLength fail.");
66             return false;
67         }
68         uint64_t charset = 0;
69         if (!decodeBuffer.DecodeInteger(charset)) {
70             TELEPHONY_LOGE("Decode encodeString DecodeInteger fail.");
71             return false;
72         }
73         charset_ = static_cast<uint32_t>(charset);
74     }
75 
76     uint32_t len = 0;
77     if (!decodeBuffer.DecodeText(strEncodeString_, len)) {
78         TELEPHONY_LOGE("Decode encodeString DecodeText fail.");
79         return false;
80     }
81     valLength_ = len;
82     return true;
83 }
84 
85 /**
86  * @brief EncodeEncodeString
87  * OMA-TS-MMS_CONF-V1_3-20110913-A   section:7.3.19 Encoded-String-Value
88  * Encoded-string-value = Text-string | Value-length Char-set Text-string
89  * End-of-string = <Octet 0>
90  * @param encodeBuffer
91  * @return true
92  * @return false
93  */
EncodeEncodeString(MmsEncodeBuffer & encodeBuffer)94 bool MmsEncodeString::EncodeEncodeString(MmsEncodeBuffer &encodeBuffer)
95 {
96     MmsEncodeBuffer tempBuffer;
97     if (charset_ != 0) {
98         if ((charset_ & 0xFF00) == 0) {
99             tempBuffer.EncodeShortInteger(charset_);
100         } else {
101             tempBuffer.EncodeInteger(charset_);
102         }
103         if (!tempBuffer.EncodeText(strEncodeString_)) {
104             TELEPHONY_LOGE("EncodeString EncodeText fail.");
105             return false;
106         }
107         if (!encodeBuffer.EncodeValueLength(tempBuffer.GetCurPosition())) {
108             TELEPHONY_LOGE("EncodeString EncodeValueLength fail.");
109             return false;
110         }
111         if (!encodeBuffer.WriteBuffer(tempBuffer)) {
112             TELEPHONY_LOGE("EncodeString WriteBuffer fail.");
113             return false;
114         }
115         return true;
116     }
117     if (!encodeBuffer.EncodeText(strEncodeString_)) {
118         TELEPHONY_LOGE("EncodeString EncodeText fail.");
119         return false;
120     }
121     return true;
122 }
123 
GetEncodeString(std::string & encodeString)124 bool MmsEncodeString::GetEncodeString(std::string &encodeString)
125 {
126     bool ret = Singleton<SmsServiceManagerClient>::GetInstance().
127         GetEncodeStringFunc(encodeString, charset_, valLength_, strEncodeString_);
128     if (encodeString.empty()) {
129         encodeString = strEncodeString_;
130     }
131     return ret;
132 }
133 
SetEncodeString(uint32_t charset,const std::string & encodeString)134 bool MmsEncodeString::SetEncodeString(uint32_t charset, const std::string &encodeString)
135 {
136     valLength_ = encodeString.length();
137     strEncodeString_ = encodeString;
138 
139     if (charset == 0) {
140         charset = CHARSET_UTF8;
141     }
142     charset_ = charset;
143     return true;
144 }
145 
SetAddressString(MmsAddress & addrsss)146 bool MmsEncodeString::SetAddressString(MmsAddress &addrsss)
147 {
148     std::string enString = addrsss.GetAddressString();
149     MmsCharSets chartSets = addrsss.GetAddressCharset();
150     return SetEncodeString(static_cast<uint32_t>(chartSets), enString);
151 }
152 
GetStrEncodeString()153 std::string MmsEncodeString::GetStrEncodeString()
154 {
155     return strEncodeString_;
156 }
157 } // namespace Telephony
158 } // namespace OHOS
159