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