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