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