• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "utils.h"
17 #include <cstdlib>
18 #include <fstream>
19 #include <sstream>
20 #include <cstring>
21 #include <sys/time.h>
22 #include <thread>
23 #include <securec.h>
24 #include "common/media_log.h"
25 
26 namespace OHOS {
27 namespace Sharing {
GetThreadId()28 unsigned long long GetThreadId()
29 {
30     std::thread::id tid = std::this_thread::get_id();
31     std::stringstream buf;
32     buf << tid;
33     std::string stid = buf.str();
34     return std::stoull(stid);
35 }
36 
Split(const std::string & s,const char * delim)37 std::vector<std::string> Split(const std::string &s, const char *delim)
38 {
39     std::vector<std::string> ret;
40     size_t last = 0;
41     auto index = s.find(delim, last);
42     while (index != std::string::npos) {
43         if (index - last > 0) {
44             ret.push_back(s.substr(last, index - last));
45         }
46         last = index + strlen(delim);
47         index = s.find(delim, last);
48     }
49     if (!s.size() || s.size() - last > 0) {
50         ret.push_back(s.substr(last));
51     }
52 
53     return ret;
54 }
55 
SplitOnce(const std::string & s,const char * delim)56 std::vector<std::string> SplitOnce(const std::string &s, const char *delim)
57 {
58     std::vector<std::string> ret;
59     size_t last = 0;
60     auto index = s.find(delim, last);
61     if (index != std::string::npos) {
62         ret.push_back(s.substr(0, index));
63         ret.push_back(s.substr(index + 1));
64     } else {
65         ret.push_back(s);
66     }
67 
68     return ret;
69 }
70 
71 #define TRIM(s, chars)                                             \
72     do {                                                           \
73         std::string map(0xFF, '\0');                               \
74         for (auto &ch : (chars)) {                                 \
75             map[(unsigned char &)ch] = '\1';                       \
76         }                                                          \
77         while ((s).size() && map.at((unsigned char &)(s).back()))  \
78             (s).pop_back();                                        \
79         while ((s).size() && map.at((unsigned char &)(s).front())) \
80             (s).erase(0, 1);                                       \
81     } while (0)
82 
Trim(std::string & s,const std::string & chars)83 std::string &Trim(std::string &s, const std::string &chars)
84 {
85     TRIM(s, chars);
86     return s;
87 }
88 
Trim(std::string && s,const std::string & chars)89 std::string Trim(std::string &&s, const std::string &chars)
90 {
91     TRIM(s, chars);
92     return std::move(s);
93 }
94 
95 #define ADD_VECTOR_END(v, i) (v).push_back((i))
96 
LowerCase(std::string value)97 std::string LowerCase(std::string value)
98 {
99     return ChangeCase(value, true);
100 }
101 
UpperCase(std::string value)102 std::string UpperCase(std::string value)
103 {
104     return ChangeCase(value, false);
105 }
106 
LTrim(std::string & value)107 void LTrim(std::string &value)
108 {
109     std::string::size_type i = 0;
110     for (i = 0; i < value.length(); i++) {
111         if (value[i] != ' ' && value[i] != '\t' && value[i] != '\n' && value[i] != '\r')
112             break;
113     }
114     value = value.substr(i);
115 }
116 
RTrim(std::string & value)117 void RTrim(std::string &value)
118 {
119     int32_t i = 0;
120     for (i = (int32_t)value.length() - 1; i >= 0; i--) {
121         if (value[i] != ' ' && value[i] != '\t' && value[i] != '\n' && value[i] != '\r')
122             break;
123     }
124     value = value.substr(0, i + 1);
125 }
126 
Trim(std::string & value)127 void Trim(std::string &value)
128 {
129     LTrim(value);
130     RTrim(value);
131 }
132 
ChangeCase(const std::string & value,bool LowerCase)133 std::string ChangeCase(const std::string &value, bool LowerCase)
134 {
135     std::string newvalue(value);
136     for (std::string::size_type i = 0, l = newvalue.length(); i < l; ++i)
137         newvalue[i] = LowerCase ? tolower(newvalue[i]) : toupper(newvalue[i]);
138     return newvalue;
139 }
140 
Replace(std::string & target,std::string search,std::string replacement)141 void Replace(std::string &target, std::string search, std::string replacement)
142 {
143     if (search == replacement) {
144         return;
145     }
146 
147     if (search == "") {
148         return;
149     }
150 
151     std::string::size_type i = std::string::npos;
152     std::string::size_type lastPos = 0;
153     while ((i = target.find(search, lastPos)) != std::string::npos) {
154         target.replace(i, search.length(), replacement);
155         lastPos = i + replacement.length();
156     }
157 }
158 
Split(std::string str,std::string separator,std::vector<std::string> & result)159 void Split(std::string str, std::string separator, std::vector<std::string> &result)
160 {
161     result.clear();
162     std::string::size_type position = str.find(separator);
163     std::string::size_type lastPosition = 0;
164     uint32_t separatorLength = separator.length();
165 
166     while (position != str.npos) {
167         ADD_VECTOR_END(result, str.substr(lastPosition, position - lastPosition));
168         lastPosition = position + separatorLength;
169         position = str.find(separator, lastPosition);
170     }
171     ADD_VECTOR_END(result, str.substr(lastPosition, std::string::npos));
172 }
173 
GetCurrentMillisecond()174 uint64_t GetCurrentMillisecond()
175 {
176     struct timeval tv {};
177     gettimeofday(&tv, nullptr);
178     return tv.tv_sec * 1000 + tv.tv_usec / 1000; // 1000: time base conversion.
179 }
180 
SaveFile(const char * data,int32_t dataSize,const std::string & fileName)181 void SaveFile(const char *data, int32_t dataSize, const std::string &fileName)
182 {
183     std::ofstream out(fileName, std::ios::out | std::ios::binary);
184     if (!out.is_open()) {
185         SHARING_LOGD("failed to opne file: %{public}s", fileName.c_str());
186         return;
187     }
188 
189     out.write(data, dataSize);
190     out.flush();
191     out.close();
192 }
193 
NeonMemcpy(volatile unsigned char * dst,volatile unsigned char * src,int size)194 void NeonMemcpy(volatile unsigned char *dst, volatile unsigned char *src, int size)
195 {
196 #if defined(__ARM_NEON__)
197     int neonCopy = size - size % 64;
198     if (neonCopy > 0) {
199         int tempCount = neonCopy;
200         asm volatile("NEONCopyPLD_%=: \n"
201                      " VLDM %[src]!,{d0-d7} \n"
202                      " VSTM %[dst]!,{d0-d7} \n"
203                      " SUBS %[tempCount],%[tempCount],#0x40 \n"
204                      " BGT NEONCopyPLD_%= \n"
205                      : [dst] "+r"(dst), [src] "+r"(src), [tempCount] "+r"(tempCount)::"d0", "d1", "d2", "d3", "d4",
206                        "d5", "d6", "d7", "cc", "memory");
207     }
208     if (size - neonCopy > 0) {
209         auto ret = memcpy_s((void *)dst, size - neonCopy, (void *)src, size - neonCopy);
210         if (ret != EOK) {
211             return;
212         }
213     }
214 #else
215     if (size > 0) {
216         auto ret = memcpy_s((void *)dst, size, (void *)src, size);
217         if (ret != EOK) {
218             return;
219         }
220     }
221 #endif
222 }
223 
SwapEndian16(uint16_t value)224 uint16_t SwapEndian16(uint16_t value)
225 {
226     return (value & 0xff00) >> 8 | (value & 0x00ff) << 8; // 8: swap endian
227 }
228 
SwapEndian32(uint32_t value)229 uint32_t SwapEndian32(uint32_t value)
230 {
231     uint8_t res[4];
232     for (int i = 0; i < 4; ++i) { // 4: swap endian
233         res[i] = ((uint8_t *)&value)[3 - i]; // 3: swap endian
234     }
235     return *(uint32_t *)res;
236 }
237 
SwapEndian64(uint64_t value)238 uint64_t SwapEndian64(uint64_t value)
239 {
240     uint8_t res[8];
241     for (int i = 0; i < 8; ++i) { // 8: swap endian
242         res[i] = ((uint8_t *)&value)[7 - i]; // 7: swap endian
243     }
244     return *(uint64_t *)res;
245 }
246 
LoadBE16(const uint8_t * p)247 uint16_t LoadBE16(const uint8_t *p)
248 {
249     return SwapEndian16(*(uint16_t *)p);
250 }
251 
LoadBE24(const uint8_t * p)252 uint32_t LoadBE24(const uint8_t *p)
253 {
254     uint8_t res[4] = {0, p[0], p[1], p[2]};
255     return SwapEndian32(*(uint32_t *)res);
256 }
257 
LoadBE32(const uint8_t * p)258 uint32_t LoadBE32(const uint8_t *p)
259 {
260     return SwapEndian32(*(uint32_t *)p);
261 }
262 
LoadBE64(const uint8_t * p)263 uint64_t LoadBE64(const uint8_t *p)
264 {
265     return SwapEndian64(*(uint64_t *)p);
266 }
267 
SetBE24(void * p,uint32_t val)268 void SetBE24(void *p, uint32_t val)
269 {
270     uint8_t *data = (uint8_t *)p;
271     data[0] = val >> 16; // 16: byte offset
272     data[1] = val >> 8; // 8: byte offset
273     data[2] = val; // 2: transformed position
274 }
275 
SetBE32(void * p,uint32_t val)276 void SetBE32(void *p, uint32_t val)
277 {
278     uint8_t *data = (uint8_t *)p;
279     data[3] = val; // 3: transformed position
280     data[2] = val >> 8; // 2: transformed position, 8: byte offset
281     data[1] = val >> 16; // 16: byte offset
282     data[0] = val >> 24; // 24: byte offset
283 }
284 
SetLE32(void * p,uint32_t val)285 void SetLE32(void *p, uint32_t val)
286 {
287     uint8_t *data = (uint8_t *)p;
288     data[0] = val;
289     data[1] = val >> 8; //  8: byte offset
290     data[2] = val >> 16; // 2: transformed position, 16: byte offset
291     data[3] = val >> 24; // 3: transformed position, 24: byte offset
292 }
293 
294 } // namespace Sharing
295 } // namespace OHOS