• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #include "medialibrary_common_utils.h"
16 
17 #include <algorithm>
18 #include <regex>
19 #include <unordered_set>
20 #include "medialibrary_errno.h"
21 #include "medialibrary_db_const.h"
22 #include "medialibrary_tracer.h"
23 #include "media_log.h"
24 #include "openssl/sha.h"
25 #include "vision_column.h"
26 
27 namespace OHOS {
28 namespace Media {
29 using namespace std;
30 const vector<string> CHAR2HEX_TABLE = {
31     "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F",
32     "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F",
33     "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F",
34     "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E", "3F",
35 
36     "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F",
37     "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F",
38     "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F",
39     "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F",
40 
41     "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F",
42     "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F",
43     "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF",
44     "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF",
45 
46     "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF",
47     "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF",
48     "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",
49     "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF"
50 };
51 
Char2Hex(const unsigned char * data,const size_t len,std::string & hexStr)52 void MediaLibraryCommonUtils::Char2Hex(const unsigned char *data, const size_t len, std::string &hexStr)
53 {
54     constexpr int CHAR_WIDTH = 8;
55     constexpr int HEX_WIDTH = 4;
56     constexpr size_t OUT_HEXSTR_SIZE = SHA256_DIGEST_LENGTH * (CHAR_WIDTH / HEX_WIDTH);
57     hexStr = "";
58     hexStr.reserve(OUT_HEXSTR_SIZE);
59     for (size_t i = 0; i < len; i++) {
60         hexStr.append(CHAR2HEX_TABLE[data[i]]);
61     }
62 }
63 
GenKey(const unsigned char * data,const size_t len,std::string & key)64 int32_t MediaLibraryCommonUtils::GenKey(const unsigned char *data, const size_t len, std::string &key)
65 {
66     if (len == 0 || len > LONG_MAX) {
67         return -EINVAL;
68     }
69 
70     unsigned char hash[SHA256_DIGEST_LENGTH] = "";
71     SHA256_CTX ctx;
72     SHA256_Init(&ctx);
73     SHA256_Update(&ctx, data, len);
74     SHA256_Final(hash, &ctx);
75 
76     /* here we translate sha256 hash to hexadecimal. each 8-bit char will be presented by two characters([0-9a-f]) */
77     Char2Hex(hash, SHA256_DIGEST_LENGTH, key);
78     return E_OK;
79 }
80 
GenKeySHA256(const std::vector<uint8_t> & input,std::string & key)81 int32_t MediaLibraryCommonUtils::GenKeySHA256(const std::vector<uint8_t> &input, std::string &key)
82 {
83     return GenKey(input.data(), input.size(), key);
84 }
85 
GenKeySHA256(const std::string & input,std::string & key)86 int32_t MediaLibraryCommonUtils::GenKeySHA256(const std::string &input, std::string &key)
87 {
88     return GenKey((const unsigned char *)input.c_str(), input.size(), key);
89 }
90 
ExtractKeyWord(std::string & str)91 void MediaLibraryCommonUtils::ExtractKeyWord(std::string &str)
92 {
93     if (str.empty()) {
94         return;
95     }
96     // add seprate space symbol,like file_id=?
97     std::regex spacePattern("\\=|\\<>|\\>|\\>=|\\<|\\<=|\\!=",
98         std::regex_constants::ECMAScript | std::regex_constants::icase);
99     str = regex_replace(str, spacePattern, " ");
100     // remove front space of key word
101     auto pos = str.find_first_not_of(" ");
102     if (pos != std::string::npos) {
103         str.erase(0, pos);
104     }
105     // remove back space of key word
106     pos = str.find_first_of(" ");
107     if (pos != std::string::npos) {
108         str = str.substr(0, pos);
109     }
110 }
111 
112 static const std::unordered_set<std::string> FILE_KEY_WHITE_LIST {
113     // Files table columns
114     MEDIA_DATA_DB_ID,
115     MEDIA_DATA_DB_RELATIVE_PATH,
116     MEDIA_DATA_DB_NAME,
117     MEDIA_DATA_DB_PARENT_ID,
118     MEDIA_DATA_DB_MIME_TYPE,
119     MEDIA_DATA_DB_MEDIA_TYPE,
120     MEDIA_DATA_DB_SIZE,
121     MEDIA_DATA_DB_DATE_ADDED,
122     MEDIA_DATA_DB_DATE_ADDED_S,
123     MEDIA_DATA_DB_DATE_MODIFIED,
124     MEDIA_DATA_DB_DATE_MODIFIED_S,
125     MEDIA_DATA_DB_DATE_TAKEN,
126     MEDIA_DATA_DB_TITLE,
127     MEDIA_DATA_DB_ARTIST,
128     MEDIA_DATA_DB_AUDIO_ALBUM,
129     MEDIA_DATA_DB_DURATION,
130     MEDIA_DATA_DB_WIDTH,
131     MEDIA_DATA_DB_HEIGHT,
132     MEDIA_DATA_DB_ORIENTATION,
133     MEDIA_DATA_DB_BUCKET_ID,
134     MEDIA_DATA_DB_BUCKET_NAME,
135     DIRECTORY_DB_DIRECTORY_TYPE,
136     MEDIA_DATA_DB_DATE_TRASHED,
137     MEDIA_DATA_DB_DATE_TRASHED_S,
138     MEDIA_DATA_DB_BUCKET_ID,
139     MEDIA_DATA_DB_ALBUM_ID,
140     DEVICE_DB_NETWORK_ID,
141     SMARTABLUMASSETS_PARENTID,
142     SMARTALBUM_DB_ID,
143     MEDIA_DATA_DB_FILE_PATH,
144     MEDIA_DATA_DB_IS_TRASH,
145     MEDIA_DATA_DB_RECYCLE_PATH,
146     MEDIA_DATA_DB_OWNER_PACKAGE,
147     MediaColumn::MEDIA_PACKAGE_NAME,
148     MEDIA_DATA_DB_IS_FAV,
149     MEDIA_DATA_DB_TIME_PENDING,
150     MEDIA_DATA_DB_POSITION,
151     PhotoColumn::PHOTO_THUMB_STATUS,
152 
153     // Photos table columns
154     COMPAT_HIDDEN,
155     COMPAT_PHOTO_SYNC_STATUS,
156     COMPAT_FILE_SUBTYPE,
157     COMPAT_CAMERA_SHOT_KEY,
158 
159     // PhotoAlbum table columns
160     COMPAT_ALBUM_SUBTYPE,
161 
162     // Analysis table columns
163     TAG_ID,
164     FACE_ID,
165     LANDMARKS,
166     FEATURE,
167     CENTER_FEATURES,
168     STATUS,
169     OCR,
170     LABEL,
171     AESTHETICS_SCORE,
172     FACE,
173     OBJECT,
174     RECOMMENDATION,
175     SEGMENTATION,
176     COMPOSITION,
177     SALIENCY,
178     CATEGORY_ID
179 };
180 
CheckWhiteList(const std::string & express)181 bool MediaLibraryCommonUtils::CheckWhiteList(const std::string &express)
182 {
183     return FILE_KEY_WHITE_LIST.find(express) != FILE_KEY_WHITE_LIST.end();
184 }
185 
CheckExpressValidation(std::vector<std::string> & sepratedStr)186 bool MediaLibraryCommonUtils::CheckExpressValidation(std::vector<std::string> &sepratedStr)
187 {
188     for (auto &str : sepratedStr) {
189         ExtractKeyWord(str);
190         if (str.empty() || (str.size() == 1 && str == " ")) {
191             continue;
192         }
193         if (!CheckWhiteList(str)) {
194             MEDIA_ERR_LOG("Failed to check key word: %{private}s", str.c_str());
195             return false;
196         }
197     }
198 
199     return true;
200 }
201 
RemoveSpecialCondition(std::string & hacker,const std::string & pattern)202 void MediaLibraryCommonUtils::RemoveSpecialCondition(std::string &hacker, const std::string &pattern)
203 {
204     auto pos = hacker.find(pattern);
205     while (pos != std::string::npos) {
206         hacker.replace(pos, pos + pattern.size(), " ");
207         pos = hacker.find(pattern);
208     }
209 }
210 
RemoveSpecialCondition(std::string & hacker)211 void MediaLibraryCommonUtils::RemoveSpecialCondition(std::string &hacker)
212 {
213     const std::string S1 = "not between ? and ?";
214     const std::string S2 = "between ? and ?";
215     const std::string S3 = "limit ?, ?";
216     RemoveSpecialCondition(hacker, S1);
217     RemoveSpecialCondition(hacker, S2);
218     RemoveSpecialCondition(hacker, S3);
219 }
220 
SeprateSelection(std::string & strCondition,std::vector<std::string> & sepratedStr)221 void MediaLibraryCommonUtils::SeprateSelection(std::string &strCondition, std::vector<std::string> &sepratedStr)
222 {
223     // 0. transform to lower
224     std::transform(strCondition.begin(), strCondition.end(), strCondition.begin(), ::tolower);
225     // 1.remove brackets
226     std::regex bracketsPattern("\\(|\\)", std::regex_constants::ECMAScript | std::regex_constants::icase);
227     strCondition = regex_replace(strCondition, bracketsPattern, "");
228 
229     // 2.remove redundant space
230     std::regex spacePattern("\\s+", std::regex_constants::ECMAScript | std::regex_constants::icase);
231     strCondition = regex_replace(strCondition, spacePattern, " ");
232 
233     // 3. remove special condition
234     RemoveSpecialCondition(strCondition);
235 
236     // 4. seprate core: according bound symbol,for example: and or ..
237     std::regex conditionPattern("\\s*and\\s+|\\s*or\\s+",
238         std::regex_constants::ECMAScript | std::regex_constants::icase);
239     std::sregex_token_iterator iter(strCondition.begin(), strCondition.end(), conditionPattern, -1);
240     decltype(iter) end;
241     while (iter != end) {
242         sepratedStr.push_back(iter->str());
243         ++iter;
244     }
245 }
246 
CheckKeyWord(const std::string & strCondition)247 bool MediaLibraryCommonUtils::CheckKeyWord(const std::string &strCondition)
248 {
249     std::regex pattern("\\s*exec\\s*|\\s*insert\\s*|\\s*delete\\s*|\\s*update\\s*|" \
250                             "\\s*join\\s*|\\s*union\\s*|\\s*master\\s*|\\s*truncate\\s*",
251                     std::regex_constants::ECMAScript | std::regex_constants::icase);
252 
253     if (regex_search(strCondition, pattern)) {
254         return false;
255     }
256 
257     return true;
258 }
259 
CheckIllegalCharacter(const std::string & strCondition)260 bool MediaLibraryCommonUtils::CheckIllegalCharacter(const std::string &strCondition)
261 {
262     /* if strCondition contains ';', it will be sepreate to two clause */
263     if (strCondition.find(';') == std::string::npos) {
264         return true;
265     }
266     /* other check to do */
267     return false;
268 }
269 
CheckWhereClause(const std::string & whereClause)270 bool MediaLibraryCommonUtils::CheckWhereClause(const std::string &whereClause)
271 {
272     MediaLibraryTracer tracer;
273     tracer.Start("CommonUtils::CheckWhereClause");
274     if (whereClause.empty() || (whereClause.size() == 1 && whereClause == " ")) {
275         return true;
276     }
277     /* check whether query condition has illegal character */
278     if (!CheckIllegalCharacter(whereClause)) {
279         MEDIA_ERR_LOG("CheckIllegalCharacter is failed!");
280         return false;
281     }
282 
283     /* check whether query condition has key word */
284     if (!CheckKeyWord(whereClause)) {
285         MEDIA_ERR_LOG("CheckKeyWord is failed!");
286         return false;
287     }
288 
289     std::vector<std::string> sepratedStr;
290     auto args = whereClause;
291     SeprateSelection(args, sepratedStr);
292     /* check every query condition */
293     return CheckExpressValidation(sepratedStr);
294 }
295 
AppendSelections(std::string & selections)296 void MediaLibraryCommonUtils::AppendSelections(std::string &selections)
297 {
298     if (selections.empty()) {
299         return;
300     }
301     selections = "(" + selections + ")";
302 }
303 } // namespace Media
304 } // namespace OHOS
305