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