1 /*
2  * Copyright (C) 2024 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 
16 #ifndef FRAMEWORKS_INNERKITSIMPL_ACCESSOR_INCLUDE_EXIF_METADATA_FORMATTER_H
17 #define FRAMEWORKS_INNERKITSIMPL_ACCESSOR_INCLUDE_EXIF_METADATA_FORMATTER_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 namespace OHOS {
24 namespace Media {
25 struct TagDetails {
26     int64_t val_;       // Tag value
27     const char *label_;
28 }; // struct TagDetails
29 
30 using ValueFormatDelegate = std::pair<std::function<int32_t (std::string&, const std::string&)>, std::string>;
31 
32 class ExifMetadatFormatter {
33 public:
34     static std::pair<int32_t, std::string> Format(const std::string &keyName, const std::string &value);
35     static int32_t Validate(const std::string &keyName, const std::string &value);
36     static bool IsModifyAllowed(const std::string &keyName);
37     static bool IsKeySupported(const std::string &keyName);
38     static bool IsSensitiveInfo(const std::string &keyName);
39     static const std::set<std::string> &GetRWKeys();
40     static const std::set<std::string> &GetROKeys();
41 
42 private:
43     static int32_t ValidateValueRange(const std::string &keyName, const std::string &value);
44     static void ConvertRangeValue(const std::string &keyName, std::string &value);
45     static void ExtractValue(const std::string &keyName, std::string &value);
46     static int32_t ConvertValueFormat(const std::string &keyName, std::string &value);
47     static int32_t ValidateValueFormat(const std::string &keyName, const std::string &value);
48     static bool IsFormatValidationConfigExisting(const std::string &keyName);
49     static bool IsForbiddenValue(const std::string &value);
Gcd(int a,int b)50     static int Gcd(int a, int b)
51     {
52         if (b == 0) {
53             return a;
54         }
55         return Gcd(b, a % b);
56     }
57     static bool IsValidValue(const TagDetails *array, const size_t &size, const int64_t &key);
58     static bool ValidRegex(const std::string &value, const std::string &regex);
59     static bool ValidRegexWithComma(std::string &value, const std::string &regex);
60     static bool ValidRegexWithRationalFormat(std::string &value, const std::string &regex);
61     static bool ValidRegexWithCommaRationalFormat(std::string &value, const std::string &regex);
62     static bool ValidRegexWithColonRationalFormat(std::string &value, const std::string &regex);
63     static bool ValidRegexWithDot(std::string &value, const std::string &regex);
64     static bool ValidRegxWithCommaDecimalRationalFormat(std::string &value, const std::string &regex);
65     static bool ValidRegexWithVersionFormat(std::string &value, const std::string &regex);
66     static bool ValidRegxAndConvertRationalFormat(std::string &value, const std::string &regex);
67     static bool ValidRegexWithDecimalRationalFormat(std::string &value, const std::string &regex);
68     static bool ValidRegexWithGpsOneRationalFormat(std::string &value, const std::string &regex);
69     static bool ValidRegexWithChannelFormat(std::string &value, const std::string &regex);
70     static void ReplaceAsSpace(std::string &value, const std::string &regex);
71     static void ReplaceAsContent(std::string &value, const std::string &regex, const std::string &content);
72     static void RationalFormat(std::string &value);
73     static std::string GetFractionFromStr(const std::string &decimal, bool &outRang);
74     static bool DecimalRationalFormat(std::string &value);
75     static bool ConvertRationalFormat(std::string &value);
76     static ValueFormatDelegate doubleIntToOneRationalWithComma;
77     static ValueFormatDelegate doubleIntWithBlank;
78     static ValueFormatDelegate doubleIntWithComma;
79     static ValueFormatDelegate doubleValueToRational;
80     static ValueFormatDelegate tribleIntWithBlank;
81     static ValueFormatDelegate tribleIntWithComma;
82     static ValueFormatDelegate fourIntWithBlank;
83     static ValueFormatDelegate fourIntWithComma;
84     static ValueFormatDelegate singleInt;
85     static ValueFormatDelegate singleRational;
86     static ValueFormatDelegate singleIntToRational;
87     static ValueFormatDelegate singleDecimalToRational;
88     static ValueFormatDelegate tribleRationalWithBlank;
89     static ValueFormatDelegate tribleIntToRationalWithBlank;
90     static ValueFormatDelegate tribleIntToRationalWithComma;
91     static ValueFormatDelegate tribleDecimalToRationalWithBlank;
92     static ValueFormatDelegate tribleDecimalToRatiionalWithComma;
93     static ValueFormatDelegate tribleMixToRationalWithComma;
94     static ValueFormatDelegate fourRationalWithBlank;
95     static ValueFormatDelegate fourIntToRationalWithBlank;
96     static ValueFormatDelegate fourIntToRationalWithComma;
97     static ValueFormatDelegate decimal4Ratiional4;
98     static ValueFormatDelegate fourDecimalToRationalWithComma;
99     static ValueFormatDelegate dateTimeValidation;
100     static ValueFormatDelegate dateValidation;
101     static ValueFormatDelegate tribleIntToRationalWithColon;
102     static ValueFormatDelegate fourIntWithDot;
103     static ValueFormatDelegate fourDecimalToRationalWithBlank;
104     static ValueFormatDelegate sixDecimalToRationalWithBlank;
105     static ValueFormatDelegate sixDecimalToRationalWithComma;
106     static ValueFormatDelegate timeStamp;
107     static ValueFormatDelegate version;
108     static ValueFormatDelegate channel;
109     static std::multimap<std::string, ValueFormatDelegate> valueFormatConvertConfig;
110     static std::multimap<std::string, std::string> valueFormatValidateConfig;
111     static std::multimap<std::string, std::string> valueTemplateConfig;
112     static std::map<std::string, std::tuple<const TagDetails*, const size_t>> valueRangeValidateConfig;
113 };
114 } // namespace Media
115 } // namespace OHOS
116 
117 #endif // FRAMEWORKS_INNERKITSIMPL_ACCESSOR_INCLUDE_EXIF_METADATA_FORMATTER_H
118