• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef EXIF_INFO_H
16 #define EXIF_INFO_H
17 #include <libexif/exif-data.h>
18 #include <map>
19 #include <string>
20 #include <vector>
21 #include "hilog/log.h"
22 #include "log_tags.h"
23 namespace OHOS {
24 namespace ImagePlugin {
25 struct DirectoryEntry {
26     ExifTag tag;
27     ExifFormat format;
28     int32_t dataCounts;   // Counts of this format data
29     uint32_t valueOffset; // Offset of Directory Entry data area from JPEG File
30     uint32_t valueLength; // Length of Directory Entry data area
31     ExifIfd ifd;          // IFD of this Entry is in
32 };
33 
34 /*
35  * Class responsible for storing and parsing EXIF information from a JPEG blob
36  */
37 class EXIFInfo {
38 public:
39     EXIFInfo();
40     ~EXIFInfo();
41     /*
42      * Parsing function for an entire JPEG image buffer.
43      * PARAM 'data': A pointer to a JPEG image.
44      * PARAM 'length': The length of the JPEG image.
45      * RETURN:  PARSE_EXIF_SUCCESS (0) on success with 'result' filled out
46      *          error code otherwise, as defined by the PARSE_EXIF_ERROR_* macros
47      */
48     int ParseExifData(const unsigned char *buf, unsigned len);
49     int ParseExifData(const std::string &data);
50     uint32_t ModifyExifData(const ExifTag &tag, const std::string &value, const std::string &path);
51     uint32_t ModifyExifData(const ExifTag &tag, const std::string &value, const int fd);
52     uint32_t ModifyExifData(const ExifTag &tag, const std::string &value, unsigned char *data, uint32_t size);
53     uint32_t GetFilterArea(const uint8_t *buf,
54                               const uint32_t &bufSize,
55                               const int &privacyType,
56                               std::vector<std::pair<uint32_t, uint32_t>> &ranges);
57     bool IsExifDataParsed();
58     uint32_t GetExifData(const std::string name, std::string &value);
59     uint32_t ModifyExifData(const std::string name, const std::string &value, const std::string &path);
60     uint32_t ModifyExifData(const std::string name, const std::string &value, const int fd);
61     uint32_t ModifyExifData(const std::string name, const std::string &value, unsigned char *data, uint32_t size);
62 
63 public:
64     static const std::string DEFAULT_EXIF_VALUE;
65     std::string bitsPerSample_; // Number of bits in each pixel of an image.
66     std::string orientation_;
67     std::string imageLength_;   // Image length.
68     std::string imageWidth_;    // mage width.
69     std::string gpsLatitude_;
70     std::string gpsLongitude_;
71     std::string gpsLatitudeRef_;
72     std::string gpsLongitudeRef_;
73     std::string dateTimeOriginal_;  // Original date and time.
74     std::string exposureTime_;
75     std::string fNumber_;
76     std::string isoSpeedRatings_;
77     std::string sceneType_;
78     std::string compressedBitsPerPixel_;
79     std::string dateTime_;
80     std::string gpsTimeStamp_;
81     std::string gpsDateStamp_;
82     std::string imageDescription_;
83     std::string make_;
84     std::string model_;
85     std::string photoMode_;
86     std::string sensitivityType_;
87     std::string standardOutputSensitivity_;
88     std::string recommendedExposureIndex_;
89     std::string apertureValue_;
90     std::string exposureBiasValue_;
91     std::string meteringMode_;
92     std::string lightSource_;
93     std::string flash_;
94     std::string focalLength_;
95     std::string userComment_;
96     std::string pixelXDimension_;
97     std::string pixelYDimension_;
98     std::string whiteBalance_;
99     std::string focalLengthIn35mmFilm_;
100     std::string hwMnoteCaptureMode_;
101     std::string hwMnotePhysicalAperture_;
102 
103 private:
104     void SetExifTagValues(const ExifTag &tag, const std::string &value);
105     void SetExifTagValuesEx(const ExifTag &tag, const std::string &value);
106     ExifEntry* InitExifTag(ExifData *exif, ExifIfd ifd, ExifTag tag);
107     ExifEntry* CreateExifTag(ExifData *exif, ExifIfd ifd, ExifTag tag, size_t len, ExifFormat format);
108     unsigned long GetFileSize(FILE *fp);
109     void ReleaseSource(unsigned char **ptrBuf, FILE **ptrFile);
110     bool CreateExifData(unsigned char *buf, unsigned long length, ExifData **data, bool &isNewExifData);
111     unsigned int GetOrginExifDataLength(const bool &isNewExifData, unsigned char *buf);
112     ExifByteOrder GetExifByteOrder(const bool &isNewExifData, unsigned char *buf);
113     bool CreateExifEntry(const ExifTag &tag, ExifData *data, const std::string &value,
114         ExifByteOrder order, ExifEntry **ptrEntry);
115     bool WriteExifDataToFile(ExifData *data, unsigned int orginExifDataLength, unsigned long fileLength,
116         unsigned char *buf, FILE *fp);
117     void UpdateCacheExifData(FILE *fp);
118     bool CheckExifEntryValid(const ExifIfd &ifd, const ExifTag &tag);
119     bool CheckExifEntryValidEx(const ExifIfd &ifd, const ExifTag &tag);
120     void GetAreaFromExifEntries(const int &privacyType,
121                                 const std::vector<DirectoryEntry> &entryArray,
122                                 std::vector<std::pair<uint32_t, uint32_t>> &ranges);
123 
124 private:
125     ExifIfd imageFileDirectory_;
126     ExifData* exifData_;
127     bool isExifDataParsed_;
128     std::map<ExifTag, std::string> exifTags_;
129 };
130 
131 class ByteOrderedBuffer {
132 public:
133     ByteOrderedBuffer(const uint8_t *fileBuf, uint32_t bufferLength);
134     ~ByteOrderedBuffer();
135     void GenerateDEArray();
136 
137 public:
138     ExifByteOrder byteOrder_ = EXIF_BYTE_ORDER_MOTOROLA;
139     const uint8_t *buf_;
140     uint32_t bufferLength_ = 0;
141     uint32_t curPosition_ = 0;
142     std::vector<DirectoryEntry> directoryEntryArray_;
143     std::vector<uint32_t> handledIfdOffsets_;
144 
145 private:
146     void GetDataRangeFromIFD(const ExifIfd &ifd);
147     void GetDataRangeFromDE(const ExifIfd &ifd, const int16_t &count);
148     int32_t ReadInt32();
149     uint32_t ReadUnsignedInt32();
150     int16_t ReadShort();
151     uint16_t ReadUnsignedShort();
152     uint32_t Peek();
153     bool SetDEDataByteCount(const uint16_t &tagNumber,
154                             const uint16_t &dataFormat,
155                             const int32_t &numberOfComponents,
156                             uint32_t &count);
157     bool IsValidTagNumber(const uint16_t &tagNumber);
158     bool IsIFDhandled(const uint32_t &position);
159     bool IsIFDPointerTag(const uint16_t &tagNumber);
160     ExifIfd GetIFDOfIFDPointerTag(const uint16_t &tagNumber);
161     ExifIfd GetNextIfdFromLinkList(const ExifIfd &ifd);
162     void ParseIFDPointerTag(const ExifIfd &ifd, const uint16_t &dataFormat);
163     uint32_t TransformTiffOffsetToFilePos(const uint32_t &offset);
164 };
165 } // namespace ImagePlugin
166 } // namespace OHOS
167 #endif // EXIF_INFO_H
168