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
16 #include "exif_info.h"
17
18 #include <algorithm>
19 #include <cstdio>
20 #include <memory>
21 #include <unistd.h>
22
23 #include "exif_maker_note.h"
24 #include "image_log.h"
25 #include "media_errors.h"
26 #include "securec.h"
27 #include "string_ex.h"
28
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN
31
32 #undef LOG_TAG
33 #define LOG_TAG "exifInfo"
34
35 namespace OHOS {
36 namespace ImagePlugin {
37 namespace {
38 static constexpr int PARSE_EXIF_SUCCESS = 0;
39 static constexpr int PARSE_EXIF_DATA_ERROR = 10001;
40 static constexpr int PARSE_EXIF_IFD_ERROR = 10002;
41 static constexpr int BUFFER_POSITION_4 = 4;
42 static constexpr int BUFFER_POSITION_5 = 5;
43 static constexpr int BUFFER_POSITION_6 = 6;
44 static constexpr int BUFFER_POSITION_7 = 7;
45 static constexpr int BUFFER_POSITION_8 = 8;
46 static constexpr int BUFFER_POSITION_9 = 9;
47 static constexpr int BUFFER_POSITION_12 = 12;
48 static constexpr int BUFFER_POSITION_13 = 13;
49 static constexpr int LENGTH_OFFSET_2 = 2;
50 static constexpr int BYTE_COUNTS_12 = 12;
51 static constexpr int MOVE_OFFSET_8 = 8;
52 static constexpr int MOVE_OFFSET_16 = 16;
53 static constexpr int MOVE_OFFSET_24 = 24;
54 static constexpr int CONSTANT_0 = 0;
55 static constexpr int CONSTANT_1 = 1;
56 static constexpr int CONSTANT_2 = 2;
57 static constexpr int CONSTANT_3 = 3;
58 static constexpr int CONSTANT_4 = 4;
59 static constexpr unsigned long MAX_FILE_SIZE = 1000 * 1000 * 1000;
60 static constexpr unsigned long GPS_DIGIT_NUMBER = 1e6;
61 static constexpr uint32_t GPS_DMS_COUNT = 3;
62 static constexpr double GPS_MAX_LATITUDE = 90.0;
63 static constexpr double GPS_MIN_LATITUDE = 0.0;
64 static constexpr double GPS_MAX_LONGITUDE = 180.0;
65 static constexpr double GPS_MIN_LONGITUDE = 0.0;
66 static constexpr uint32_t ERROR_PARSE_EXIF_FAILED = 1;
67 static constexpr uint32_t ERROR_NO_EXIF_TAGS = 2;
68 static constexpr ExifTag TAG_SENSITIVITY_TYPE = static_cast<ExifTag>(0x8830);
69 static constexpr ExifTag TAG_STANDARD_OUTPUT_SENSITIVITY = static_cast<ExifTag>(0x8831);
70 static constexpr ExifTag TAG_RECOMMENDED_EXPOSURE_INDEX = static_cast<ExifTag>(0x8832);
71 static constexpr size_t SIZE_1 = 1;
72 static constexpr size_t SIZE_2 = 2;
73 static constexpr size_t SIZE_3 = 3;
74
75 /* raw EXIF header data */
76 static const unsigned char EXIF_HEADER[] = {
77 0xff, 0xd8, 0xff, 0xe1
78 };
79 /* Offset of tiff begin from jpeg file begin */
80 static constexpr uint32_t TIFF_OFFSET_FROM_FILE_BEGIN = 12;
81 static constexpr int PERMISSION_GPS_TYPE = 1;
82
83 static const struct TagEntry {
84 /*! Tag ID. There may be duplicate tags when the same number is used for
85 * different meanings in different IFDs. */
86 ExifTag tag;
87 const std::string name;
88 const uint16_t number;
89 } EXIF_TAG_TABLE[] = {
90 {EXIF_TAG_GPS_VERSION_ID, "GPSVersionID", 0x0000},
91 {EXIF_TAG_INTEROPERABILITY_INDEX, "InteroperabilityIndex", 0x0001},
92 {EXIF_TAG_GPS_LATITUDE_REF, "GPSLatitudeRef", 0x0001},
93 {EXIF_TAG_INTEROPERABILITY_VERSION, "InteroperabilityVersion", 0x0002},
94 {EXIF_TAG_GPS_LATITUDE, "GPSLatitude", 0x0002},
95 {EXIF_TAG_GPS_LONGITUDE_REF, "GPSLongitudeRef", 0x0003},
96 {EXIF_TAG_GPS_LONGITUDE, "GPSLongitude", 0x0004},
97 {EXIF_TAG_GPS_ALTITUDE_REF, "GPSAltitudeRef", 0x0005},
98 {EXIF_TAG_GPS_ALTITUDE, "GPSAltitude", 0x0006},
99 {EXIF_TAG_GPS_TIME_STAMP, "GPSTimeStamp", 0x0007},
100 {EXIF_TAG_GPS_SATELLITES, "GPSSatellites", 0x0008},
101 {EXIF_TAG_GPS_STATUS, "GPSStatus", 0x0009},
102 {EXIF_TAG_GPS_MEASURE_MODE, "GPSMeasureMode", 0x000a},
103 {EXIF_TAG_GPS_DOP, "GPSDOP", 0x000b},
104 {EXIF_TAG_GPS_SPEED_REF, "GPSSpeedRef", 0x000c},
105 {EXIF_TAG_GPS_SPEED, "GPSSpeed", 0x000d},
106 {EXIF_TAG_GPS_TRACK_REF, "GPSTrackRef", 0x000e},
107 {EXIF_TAG_GPS_TRACK, "GPSTrack", 0x000f},
108 {EXIF_TAG_GPS_IMG_DIRECTION_REF, "GPSImgDirectionRef", 0x0010},
109 {EXIF_TAG_GPS_IMG_DIRECTION, "GPSImgDirection", 0x0011},
110 {EXIF_TAG_GPS_MAP_DATUM, "GPSMapDatum", 0x0012},
111 {EXIF_TAG_GPS_DEST_LATITUDE_REF, "GPSDestLatitudeRef", 0x0013},
112 {EXIF_TAG_GPS_DEST_LATITUDE, "GPSDestLatitude", 0x0014},
113 {EXIF_TAG_GPS_DEST_LONGITUDE_REF, "GPSDestLongitudeRef", 0x0015},
114 {EXIF_TAG_GPS_DEST_LONGITUDE, "GPSDestLongitude", 0x0016},
115 {EXIF_TAG_GPS_DEST_BEARING_REF, "GPSDestBearingRef", 0x0017},
116 {EXIF_TAG_GPS_DEST_BEARING, "GPSDestBearing", 0x0018},
117 {EXIF_TAG_GPS_DEST_DISTANCE_REF, "GPSDestDistanceRef", 0x0019},
118 {EXIF_TAG_GPS_DEST_DISTANCE, "GPSDestDistance", 0x001a},
119 {EXIF_TAG_GPS_PROCESSING_METHOD, "GPSProcessingMethod", 0x001b},
120 {EXIF_TAG_GPS_AREA_INFORMATION, "GPSAreaInformation", 0x001c},
121 {EXIF_TAG_GPS_DATE_STAMP, "GPSDateStamp", 0x001d},
122 {EXIF_TAG_GPS_DIFFERENTIAL, "GPSDifferential", 0x001e},
123 {EXIF_TAG_GPS_H_POSITIONING_ERROR, "GPSHPositioningError", 0x001f},
124 /* Not in EXIF 2.2 */
125 {EXIF_TAG_NEW_SUBFILE_TYPE, "NewSubfileType", 0x00fe},
126 {EXIF_TAG_IMAGE_WIDTH, "ImageWidth", 0x0100},
127 {EXIF_TAG_IMAGE_LENGTH, "ImageLength", 0x0101},
128 {EXIF_TAG_BITS_PER_SAMPLE, "BitsPerSample", 0x0102},
129 {EXIF_TAG_COMPRESSION, "Compression", 0x0103},
130 {EXIF_TAG_PHOTOMETRIC_INTERPRETATION, "PhotometricInterpretation", 0x0106},
131 /* Not in EXIF 2.2 */
132 {EXIF_TAG_FILL_ORDER, "FillOrder", 0x010a},
133 /* Not in EXIF 2.2 */
134 {EXIF_TAG_DOCUMENT_NAME, "DocumentName", 0x010d},
135 {EXIF_TAG_IMAGE_DESCRIPTION, "ImageDescription", 0x010e},
136 {EXIF_TAG_MAKE, "Make", 0x010f},
137 {EXIF_TAG_MODEL, "Model", 0x0110},
138 {EXIF_TAG_STRIP_OFFSETS, "StripOffsets", 0x0111},
139 {EXIF_TAG_ORIENTATION, "Orientation", 0x0112},
140 {EXIF_TAG_SAMPLES_PER_PIXEL, "SamplesPerPixel", 0x0115},
141 {EXIF_TAG_ROWS_PER_STRIP, "RowsPerStrip", 0x0116},
142 {EXIF_TAG_STRIP_BYTE_COUNTS, "StripByteCounts", 0x0117},
143 {EXIF_TAG_X_RESOLUTION, "XResolution", 0x011a},
144 {EXIF_TAG_Y_RESOLUTION, "YResolution", 0x011b},
145 {EXIF_TAG_PLANAR_CONFIGURATION, "PlanarConfiguration", 0x011c},
146 {EXIF_TAG_RESOLUTION_UNIT, "ResolutionUnit", 0x0128},
147 {EXIF_TAG_TRANSFER_FUNCTION, "TransferFunction", 0x012d},
148 {EXIF_TAG_SOFTWARE, "Software", 0x0131},
149 {EXIF_TAG_DATE_TIME, "DateTime", 0x0132},
150 {EXIF_TAG_ARTIST, "Artist", 0x013b},
151 {EXIF_TAG_WHITE_POINT, "WhitePoint", 0x013e},
152 {EXIF_TAG_PRIMARY_CHROMATICITIES, "PrimaryChromaticities", 0x013f},
153 /* Not in EXIF 2.2 */
154 {EXIF_TAG_SUB_IFDS, "SubIFDs", 0x014a},
155 /* Not in EXIF 2.2 */
156 {EXIF_TAG_TRANSFER_RANGE, "TransferRange", 0x0156},
157 /* Not in EXIF 2.2 */
158 {EXIF_TAG_JPEG_INTERCHANGE_FORMAT, "JPEGInterchangeFormat", 0x0201},
159 {EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH, "JPEGInterchangeFormatLength", 0x0202},
160 {EXIF_TAG_YCBCR_COEFFICIENTS, "YCbCrCoefficients", 0x0211},
161 {EXIF_TAG_YCBCR_SUB_SAMPLING, "YCbCrSubSampling", 0x0212},
162 {EXIF_TAG_YCBCR_POSITIONING, "YCbCrPositioning", 0x0213},
163 {EXIF_TAG_REFERENCE_BLACK_WHITE, "ReferenceBlackWhite", 0x0214},
164 /* Not in EXIF 2.2 */
165 {EXIF_TAG_XML_PACKET, "XMLPacket", 0x02bc},
166 /* Not in EXIF 2.2 */
167 {EXIF_TAG_RELATED_IMAGE_FILE_FORMAT, "RelatedImageFileFormat", 0x1000},
168 /* Not in EXIF 2.2 */
169 {EXIF_TAG_RELATED_IMAGE_WIDTH, "RelatedImageWidth", 0x1001},
170 /* Not in EXIF 2.2 */
171 {EXIF_TAG_RELATED_IMAGE_LENGTH, "RelatedImageLength", 0x1002},
172 /* Not in EXIF 2.2 */
173 {EXIF_TAG_CFA_REPEAT_PATTERN_DIM, "CFARepeatPatternDim", 0x828d},
174 /* Not in EXIF 2.2 */
175 {EXIF_TAG_CFA_PATTERN, "CFAPattern", 0x828e},
176 /* Not in EXIF 2.2 */
177 {EXIF_TAG_BATTERY_LEVEL, "BatteryLevel", 0x828f},
178 {EXIF_TAG_COPYRIGHT, "Copyright", 0x8298},
179 {EXIF_TAG_EXPOSURE_TIME, "ExposureTime", 0x829a},
180 {EXIF_TAG_FNUMBER, "FNumber", 0x829d},
181 /* Not in EXIF 2.2 */
182 {EXIF_TAG_IPTC_NAA, "IPTC/NAA", 0x83bb},
183 /* Not in EXIF 2.2 */
184 {EXIF_TAG_IMAGE_RESOURCES, "ImageResources", 0x8649},
185 {EXIF_TAG_EXIF_IFD_POINTER, "ExifIfdPointer", 0x8769},
186 /* Not in EXIF 2.2 */
187 {EXIF_TAG_INTER_COLOR_PROFILE, "InterColorProfile", 0x8773},
188 {EXIF_TAG_EXPOSURE_PROGRAM, "ExposureProgram", 0x8822},
189 {EXIF_TAG_SPECTRAL_SENSITIVITY, "SpectralSensitivity", 0x8824},
190 {EXIF_TAG_GPS_INFO_IFD_POINTER, "GPSInfoIFDPointer", 0x8825},
191 {EXIF_TAG_ISO_SPEED_RATINGS, "ISOSpeedRatings", 0x8827},
192 {EXIF_TAG_OECF, "OECF", 0x8828},
193 /* Not in EXIF 2.2 */
194 {EXIF_TAG_TIME_ZONE_OFFSET, "TimeZoneOffset", 0x882a},
195 {TAG_SENSITIVITY_TYPE, "SensitivityType", 0x8830},
196 {TAG_STANDARD_OUTPUT_SENSITIVITY, "StandardOutputSensitivity", 0x8831},
197 {TAG_RECOMMENDED_EXPOSURE_INDEX, "RecommendedExposureIndex", 0x8832},
198 {EXIF_TAG_EXIF_VERSION, "ExifVersion", 0x9000},
199 {EXIF_TAG_DATE_TIME_ORIGINAL, "DateTimeOriginal", 0x9003},
200 {EXIF_TAG_DATE_TIME_DIGITIZED, "DateTimeDigitized", 0x9004},
201 {EXIF_TAG_COMPONENTS_CONFIGURATION, "ComponentsConfiguration", 0x9101},
202 {EXIF_TAG_COMPRESSED_BITS_PER_PIXEL, "CompressedBitsPerPixel", 0x9102},
203 {EXIF_TAG_SHUTTER_SPEED_VALUE, "ShutterSpeedValue", 0x9201},
204 {EXIF_TAG_APERTURE_VALUE, "ApertureValue", 0x9202},
205 {EXIF_TAG_BRIGHTNESS_VALUE, "BrightnessValue", 0x9203},
206 {EXIF_TAG_EXPOSURE_BIAS_VALUE, "ExposureBiasValue", 0x9204},
207 {EXIF_TAG_MAX_APERTURE_VALUE, "MaxApertureValue", 0x9205},
208 {EXIF_TAG_SUBJECT_DISTANCE, "SubjectDistance", 0x9206},
209 {EXIF_TAG_METERING_MODE, "MeteringMode", 0x9207},
210 {EXIF_TAG_LIGHT_SOURCE, "LightSource", 0x9208},
211 {EXIF_TAG_FLASH, "Flash", 0x9209},
212 {EXIF_TAG_FOCAL_LENGTH, "FocalLength", 0x920a},
213 {EXIF_TAG_SUBJECT_AREA, "SubjectArea", 0x9214},
214 /* Not in EXIF 2.2 */
215 {EXIF_TAG_TIFF_EP_STANDARD_ID, "TIFF/EPStandardID", 0x9216},
216 {EXIF_TAG_MAKER_NOTE, "MakerNote", 0x927c},
217 {EXIF_TAG_USER_COMMENT, "UserComment", 0x9286},
218 {EXIF_TAG_SUB_SEC_TIME, "SubsecTime", 0x9290},
219 {EXIF_TAG_SUB_SEC_TIME_ORIGINAL, "SubSecTimeOriginal", 0x9291},
220 {EXIF_TAG_SUB_SEC_TIME_DIGITIZED, "SubSecTimeDigitized", 0x9292},
221 /* Not in EXIF 2.2 (Microsoft extension) */
222 {EXIF_TAG_XP_TITLE, "XPTitle", 0x9c9b},
223 /* Not in EXIF 2.2 (Microsoft extension) */
224 {EXIF_TAG_XP_COMMENT, "XPComment", 0x9c9c},
225 /* Not in EXIF 2.2 (Microsoft extension) */
226 {EXIF_TAG_XP_AUTHOR, "XPAuthor", 0x9c9d},
227 /* Not in EXIF 2.2 (Microsoft extension) */
228 {EXIF_TAG_XP_KEYWORDS, "XPKeywords", 0x9c9e},
229 /* Not in EXIF 2.2 (Microsoft extension) */
230 {EXIF_TAG_XP_SUBJECT, "XPSubject", 0x9c9f},
231 {EXIF_TAG_FLASH_PIX_VERSION, "FlashpixVersion", 0xa000},
232 {EXIF_TAG_COLOR_SPACE, "ColorSpace", 0xa001},
233 {EXIF_TAG_PIXEL_X_DIMENSION, "PixelXDimension", 0xa002},
234 {EXIF_TAG_PIXEL_Y_DIMENSION, "PixelYDimension", 0xa003},
235 {EXIF_TAG_RELATED_SOUND_FILE, "RelatedSoundFile", 0xa004},
236 {EXIF_TAG_INTEROPERABILITY_IFD_POINTER, "InteroperabilityIFDPointer", 0xa005},
237 {EXIF_TAG_FLASH_ENERGY, "FlashEnergy", 0xa20b},
238 {EXIF_TAG_SPATIAL_FREQUENCY_RESPONSE, "SpatialFrequencyResponse", 0xa20c},
239 {EXIF_TAG_FOCAL_PLANE_X_RESOLUTION, "FocalPlaneXResolution", 0xa20e},
240 {EXIF_TAG_FOCAL_PLANE_Y_RESOLUTION, "FocalPlaneYResolution", 0xa20f},
241 {EXIF_TAG_FOCAL_PLANE_RESOLUTION_UNIT, "FocalPlaneResolutionUnit", 0xa210},
242 {EXIF_TAG_SUBJECT_LOCATION, "SubjectLocation", 0xa214},
243 {EXIF_TAG_EXPOSURE_INDEX, "ExposureIndex", 0xa215},
244 {EXIF_TAG_SENSING_METHOD, "SensingMethod", 0xa217},
245 {EXIF_TAG_FILE_SOURCE, "FileSource", 0xa300},
246 {EXIF_TAG_SCENE_TYPE, "SceneType", 0xa301},
247 {EXIF_TAG_NEW_CFA_PATTERN, "CFAPattern", 0xa302},
248 {EXIF_TAG_CUSTOM_RENDERED, "CustomRendered", 0xa401},
249 {EXIF_TAG_EXPOSURE_MODE, "ExposureMode", 0xa402},
250 {EXIF_TAG_WHITE_BALANCE, "WhiteBalance", 0xa403},
251 {EXIF_TAG_DIGITAL_ZOOM_RATIO, "DigitalZoomRatio", 0xa404},
252 {EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM, "FocalLengthIn35mmFilm", 0xa405},
253 {EXIF_TAG_SCENE_CAPTURE_TYPE, "SceneCaptureType", 0xa406},
254 {EXIF_TAG_GAIN_CONTROL, "GainControl", 0xa407},
255 {EXIF_TAG_CONTRAST, "Contrast", 0xa408},
256 {EXIF_TAG_SATURATION, "Saturation", 0xa409},
257 {EXIF_TAG_SHARPNESS, "Sharpness", 0xa40a},
258 {EXIF_TAG_DEVICE_SETTING_DESCRIPTION, "DeviceSettingDescription", 0xa40b},
259 {EXIF_TAG_SUBJECT_DISTANCE_RANGE, "SubjectDistanceRange", 0xa40c},
260 {EXIF_TAG_IMAGE_UNIQUE_ID, "ImageUniqueID", 0xa420},
261 /* EXIF 2.3 */
262 {EXIF_TAG_CAMERA_OWNER_NAME, "CameraOwnerName", 0xa430},
263 /* EXIF 2.3 */
264 {EXIF_TAG_BODY_SERIAL_NUMBER, "BodySerialNumber", 0xa431},
265 /* EXIF 2.3 */
266 {EXIF_TAG_LENS_SPECIFICATION, "LensSpecification", 0xa432},
267 /* EXIF 2.3 */
268 {EXIF_TAG_LENS_MAKE, "LensMake", 0xa433},
269 /* EXIF 2.3 */
270 {EXIF_TAG_LENS_MODEL, "LensModel", 0xa434},
271 /* EXIF 2.3 */
272 {EXIF_TAG_LENS_SERIAL_NUMBER, "LensSerialNumber", 0xa435},
273 /* EXIF 2.32 */
274 {EXIF_TAG_COMPOSITE_IMAGE, "CompositeImage", 0xa460},
275 /* EXIF 2.32 */
276 {EXIF_TAG_SOURCE_IMAGE_NUMBER_OF_COMPOSITE_IMAGE, "SourceImageNumberOfCompositeImage", 0xa461},
277 /* EXIF 2.32 */
278 {EXIF_TAG_SOURCE_EXPOSURE_TIMES_OF_COMPOSITE_IMAGE, "SourceExposureTimesOfCompositeImage", 0xa462},
279 /* EXIF 2.3 */
280 {EXIF_TAG_GAMMA, "Gamma", 0xa500},
281 /* Not in EXIF 2.2 */
282 {EXIF_TAG_PRINT_IMAGE_MATCHING, "PrintImageMatching", 0xc4a5},
283 /* Not in EXIF 2.2 (from the Microsoft HD Photo specification) */
284 {EXIF_TAG_PADDING, "Padding", 0xea1c},
285 {static_cast<ExifTag>(0xffff), "", 0xffff}};
286 }
287
288 const std::string EXIFInfo::DEFAULT_EXIF_VALUE = "default_exif_value";
289
290 const std::string DATE_TIME_ORIGINAL = "DateTimeOriginal";
291 const std::string DATE_TIME_ORIGINAL_MEDIA = "DateTimeOriginalForMedia";
292 const std::string TAG_ORIENTATION_STRING = "Orientation";
293 const std::string TAG_ORIENTATION_INT = "OrientationInt";
294 const std::map<ExifTag, std::string> TAG_MAP = {
295 {ExifTag::EXIF_TAG_BITS_PER_SAMPLE, "BitsPerSample"},
296 {ExifTag::EXIF_TAG_ORIENTATION, TAG_ORIENTATION_STRING},
297 {ExifTag::EXIF_TAG_IMAGE_LENGTH, "ImageLength"},
298 {ExifTag::EXIF_TAG_IMAGE_WIDTH, "ImageWidth"},
299 {ExifTag::EXIF_TAG_GPS_LATITUDE, "GPSLatitude"},
300 {ExifTag::EXIF_TAG_GPS_LONGITUDE, "GPSLongitude"},
301 {ExifTag::EXIF_TAG_GPS_LATITUDE_REF, "GPSLatitudeRef"},
302 {ExifTag::EXIF_TAG_GPS_LONGITUDE_REF, "GPSLongitudeRef"},
303 {ExifTag::EXIF_TAG_DATE_TIME_ORIGINAL, DATE_TIME_ORIGINAL},
304 {ExifTag::EXIF_TAG_EXPOSURE_TIME, "ExposureTime"},
305 {ExifTag::EXIF_TAG_FNUMBER, "FNumber"},
306 {ExifTag::EXIF_TAG_ISO_SPEED_RATINGS, "ISOSpeedRatings"},
307 {ExifTag::EXIF_TAG_SCENE_TYPE, "SceneType"},
308 {ExifTag::EXIF_TAG_COMPRESSED_BITS_PER_PIXEL, "CompressedBitsPerPixel"},
309 {ExifTag::EXIF_TAG_DATE_TIME, "DateTime"},
310 {ExifTag::EXIF_TAG_GPS_TIME_STAMP, "GPSTimeStamp"},
311 {ExifTag::EXIF_TAG_GPS_DATE_STAMP, "GPSDateStamp"},
312 {ExifTag::EXIF_TAG_IMAGE_DESCRIPTION, "ImageDescription"},
313 {ExifTag::EXIF_TAG_MAKE, "Make"},
314 {ExifTag::EXIF_TAG_MODEL, "Model"},
315 {ExifTag::EXIF_TAG_JPEG_PROC, "PhotoMode"},
316 {ExifTag::EXIF_TAG_SENSITIVITY_TYPE, "SensitivityType"},
317 {ExifTag::EXIF_TAG_STANDARD_OUTPUT_SENSITIVITY, "StandardOutputSensitivity"},
318 {ExifTag::EXIF_TAG_RECOMMENDED_EXPOSURE_INDEX, "RecommendedExposureIndex"},
319 {ExifTag::EXIF_TAG_ISO_SPEED, "ISOSpeedRatings"},
320 {ExifTag::EXIF_TAG_APERTURE_VALUE, "ApertureValue"},
321 {ExifTag::EXIF_TAG_EXPOSURE_BIAS_VALUE, "ExposureBiasValue"},
322 {ExifTag::EXIF_TAG_METERING_MODE, "MeteringMode"},
323 {ExifTag::EXIF_TAG_LIGHT_SOURCE, "LightSource"},
324 {ExifTag::EXIF_TAG_FLASH, "Flash"},
325 {ExifTag::EXIF_TAG_FOCAL_LENGTH, "FocalLength"},
326 {ExifTag::EXIF_TAG_USER_COMMENT, "UserComment"},
327 {ExifTag::EXIF_TAG_PIXEL_X_DIMENSION, "PixelXDimension"},
328 {ExifTag::EXIF_TAG_PIXEL_Y_DIMENSION, "PixelYDimension"},
329 {ExifTag::EXIF_TAG_WHITE_BALANCE, "WhiteBalance"},
330 {ExifTag::EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM, "FocalLengthIn35mmFilm"},
331 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_CAPTURE_MODE), "HwMnoteCaptureMode"},
332 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_PHYSICAL_APERTURE), "HwMnotePhysicalAperture"},
333 };
334
335 const static std::map<ExifTag, std::string> TAG_MAKER_NOTE_MAP = {
336 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_ROLL_ANGLE), "HwMnoteRollAngle"},
337 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_PITCH_ANGLE), "HwMnotePitchAngle"},
338 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_FOOD_CONF), "HwMnoteSceneFoodConf"},
339 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_STAGE_CONF), "HwMnoteSceneStageConf"},
340 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_BLUE_SKY_CONF), "HwMnoteSceneBlueSkyConf"},
341 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_GREEN_PLANT_CONF), "HwMnoteSceneGreenPlantConf"},
342 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_BEACH_CONF), "HwMnoteSceneBeachConf"},
343 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_SNOW_CONF), "HwMnoteSceneSnowConf"},
344 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_SUNSET_CONF), "HwMnoteSceneSunsetConf"},
345 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_FLOWERS_CONF), "HwMnoteSceneFlowersConf"},
346 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_NIGHT_CONF), "HwMnoteSceneNightConf"},
347 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_SCENE_TEXT_CONF), "HwMnoteSceneTextConf"},
348 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_FACE_COUNT), "HwMnoteFaceCount"},
349 {static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_FOCUS_MODE), "HwMnoteFocusMode"},
350 };
351
352 static const std::map<std::string, uint32_t> ORIENTATION_INT_MAP = {
353 {"Top-left", 0},
354 {"Bottom-right", 180},
355 {"Right-top", 90},
356 {"Left-bottom", 270},
357 };
358
EXIFInfo()359 EXIFInfo::EXIFInfo()
360 : bitsPerSample_(DEFAULT_EXIF_VALUE),
361 orientation_(DEFAULT_EXIF_VALUE),
362 imageLength_(DEFAULT_EXIF_VALUE),
363 imageWidth_(DEFAULT_EXIF_VALUE),
364 gpsLatitude_(DEFAULT_EXIF_VALUE),
365 gpsLongitude_(DEFAULT_EXIF_VALUE),
366 gpsLatitudeRef_(DEFAULT_EXIF_VALUE),
367 gpsLongitudeRef_(DEFAULT_EXIF_VALUE),
368 dateTimeOriginal_(DEFAULT_EXIF_VALUE),
369 exposureTime_(DEFAULT_EXIF_VALUE),
370 fNumber_(DEFAULT_EXIF_VALUE),
371 isoSpeedRatings_(DEFAULT_EXIF_VALUE),
372 sceneType_(DEFAULT_EXIF_VALUE),
373 compressedBitsPerPixel_(DEFAULT_EXIF_VALUE),
374 dateTime_(DEFAULT_EXIF_VALUE),
375 gpsTimeStamp_(DEFAULT_EXIF_VALUE),
376 gpsDateStamp_(DEFAULT_EXIF_VALUE),
377 imageDescription_(DEFAULT_EXIF_VALUE),
378 make_(DEFAULT_EXIF_VALUE),
379 model_(DEFAULT_EXIF_VALUE),
380 photoMode_(DEFAULT_EXIF_VALUE),
381 sensitivityType_(DEFAULT_EXIF_VALUE),
382 standardOutputSensitivity_(DEFAULT_EXIF_VALUE),
383 recommendedExposureIndex_(DEFAULT_EXIF_VALUE),
384 apertureValue_(DEFAULT_EXIF_VALUE),
385 exposureBiasValue_(DEFAULT_EXIF_VALUE),
386 meteringMode_(DEFAULT_EXIF_VALUE),
387 lightSource_(DEFAULT_EXIF_VALUE),
388 flash_(DEFAULT_EXIF_VALUE),
389 focalLength_(DEFAULT_EXIF_VALUE),
390 userComment_(DEFAULT_EXIF_VALUE),
391 pixelXDimension_(DEFAULT_EXIF_VALUE),
392 pixelYDimension_(DEFAULT_EXIF_VALUE),
393 whiteBalance_(DEFAULT_EXIF_VALUE),
394 focalLengthIn35mmFilm_(DEFAULT_EXIF_VALUE),
395 hwMnoteCaptureMode_(DEFAULT_EXIF_VALUE),
396 hwMnotePhysicalAperture_(DEFAULT_EXIF_VALUE),
397 hwMnoteRollAngle_(DEFAULT_EXIF_VALUE),
398 hwMnotePitchAngle_(DEFAULT_EXIF_VALUE),
399 hwMnoteSceneFoodConf_(DEFAULT_EXIF_VALUE),
400 hwMnoteSceneStageConf_(DEFAULT_EXIF_VALUE),
401 hwMnoteSceneBlueSkyConf_(DEFAULT_EXIF_VALUE),
402 hwMnoteSceneGreenPlantConf_(DEFAULT_EXIF_VALUE),
403 hwMnoteSceneBeachConf_(DEFAULT_EXIF_VALUE),
404 hwMnoteSceneSnowConf_(DEFAULT_EXIF_VALUE),
405 hwMnoteSceneSunsetConf_(DEFAULT_EXIF_VALUE),
406 hwMnoteSceneFlowersConf_(DEFAULT_EXIF_VALUE),
407 hwMnoteSceneNightConf_(DEFAULT_EXIF_VALUE),
408 hwMnoteSceneTextConf_(DEFAULT_EXIF_VALUE),
409 hwMnoteFaceCount_(DEFAULT_EXIF_VALUE),
410 hwMnoteFocusMode_(DEFAULT_EXIF_VALUE),
411 imageFileDirectory_(EXIF_IFD_COUNT),
412 exifData_(nullptr),
413 isExifDataParsed_(false)
414 {
415 for (auto i = TAG_MAP.begin(); i != TAG_MAP.end(); i++) {
416 exifTags_[i->first] = DEFAULT_EXIF_VALUE;
417 }
418 }
419
~EXIFInfo()420 EXIFInfo::~EXIFInfo()
421 {
422 if (exifData_ != nullptr) {
423 exif_data_unref(exifData_);
424 exifData_ = nullptr;
425 }
426 exifTags_.clear();
427 }
428
DumpTagsMap(std::map<ExifTag,std::string> & tags)429 static void inline DumpTagsMap(std::map<ExifTag, std::string> &tags)
430 {
431 for (auto i = tags.begin(); i != tags.end(); i++) {
432 if (TAG_MAP.count(i->first) == 0) {
433 IMAGE_LOGD("DumpTagsMap %{public}d -> %{public}s.", i->first, i->second.c_str());
434 continue;
435 }
436 std::string name = TAG_MAP.at(i->first);
437 IMAGE_LOGD("DumpTagsMap %{public}s(%{public}d) -> %{public}s.", name.c_str(), i->first, i->second.c_str());
438 }
439 }
440
ParseExifData(const unsigned char * buf,unsigned len)441 int EXIFInfo::ParseExifData(const unsigned char *buf, unsigned len)
442 {
443 IMAGE_LOGD("ParseExifData ENTER");
444 if (exifData_ != nullptr) {
445 exif_data_unref(exifData_);
446 exifData_ = nullptr;
447 }
448 exifData_ = exif_data_new();
449 if (!exifData_) {
450 return PARSE_EXIF_DATA_ERROR;
451 }
452 exif_data_unset_option(exifData_, EXIF_DATA_OPTION_IGNORE_UNKNOWN_TAGS);
453 exif_data_load_data (exifData_, buf, len);
454 exif_data_foreach_content(exifData_,
455 [](ExifContent *ec, void *userData) {
456 ExifIfd ifd = exif_content_get_ifd(ec);
457 (static_cast<EXIFInfo*>(userData))->imageFileDirectory_ = ifd;
458 if (ifd == EXIF_IFD_COUNT) {
459 IMAGE_LOGD("GetIfd ERROR");
460 return;
461 }
462 exif_content_foreach_entry(ec,
463 [](ExifEntry *ee, void* userData) {
464 if (ee == nullptr) {
465 return;
466 }
467 char tagValueChar[1024];
468 exif_entry_get_value(ee, tagValueChar, sizeof(tagValueChar));
469 std::string tagValueStr(&tagValueChar[0], &tagValueChar[strlen(tagValueChar)]);
470 if ((static_cast<EXIFInfo*>(userData))->CheckExifEntryValid(exif_entry_get_ifd(ee), ee->tag)) {
471 (static_cast<EXIFInfo*>(userData))->SetExifTagValues(ee->tag, tagValueStr);
472 }
473 }, userData);
474 }, this);
475
476 if (imageFileDirectory_ == EXIF_IFD_COUNT) {
477 return PARSE_EXIF_IFD_ERROR;
478 }
479 ExifMakerNote exifMakerNote;
480 if (exifMakerNote.Parser(exifData_, buf, len) == Media::SUCCESS) {
481 SetExifTagValues(static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_CAPTURE_MODE),
482 exifMakerNote.hwCaptureMode);
483 SetExifTagValues(static_cast<ExifTag>(ExifMakerNote::HW_MNOTE_TAG_PHYSICAL_APERTURE),
484 exifMakerNote.hwPhysicalAperture);
485 }
486 makerInfoTagValueMap = exifMakerNote.makerTagValueMap;
487 isExifDataParsed_ = true;
488 DumpTagsMap(exifTags_);
489 return PARSE_EXIF_SUCCESS;
490 }
491
ParseExifData(const std::string & data)492 int EXIFInfo::ParseExifData(const std::string &data)
493 {
494 return ParseExifData((const unsigned char *)data.data(), data.length());
495 }
496
IsExifDataParsed()497 bool EXIFInfo::IsExifDataParsed()
498 {
499 return isExifDataParsed_;
500 }
501
SetExifTagValues(const ExifTag & tag,const std::string & value)502 void EXIFInfo::SetExifTagValues(const ExifTag &tag, const std::string &value)
503 {
504 exifTags_[tag] = value;
505 if (tag == EXIF_TAG_BITS_PER_SAMPLE) {
506 bitsPerSample_ = value;
507 } else if (tag == EXIF_TAG_ORIENTATION) {
508 orientation_ = value;
509 } else if (tag == EXIF_TAG_IMAGE_LENGTH) {
510 imageLength_ = value;
511 } else if (tag == EXIF_TAG_IMAGE_WIDTH) {
512 imageWidth_ = value;
513 } else if (tag == EXIF_TAG_GPS_LATITUDE) {
514 gpsLatitude_ = value;
515 } else if (tag == EXIF_TAG_GPS_LONGITUDE) {
516 gpsLongitude_ = value;
517 } else if (tag == EXIF_TAG_GPS_LATITUDE_REF) {
518 gpsLatitudeRef_ = value;
519 } else if (tag == EXIF_TAG_GPS_LONGITUDE_REF) {
520 gpsLongitudeRef_ = value;
521 } else if (tag == EXIF_TAG_DATE_TIME_ORIGINAL) {
522 dateTimeOriginal_ = value;
523 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
524 exposureTime_ = value;
525 } else if (tag == EXIF_TAG_FNUMBER) {
526 fNumber_ = value;
527 } else if (tag == EXIF_TAG_ISO_SPEED_RATINGS) {
528 isoSpeedRatings_ = value;
529 } else if (tag == EXIF_TAG_SCENE_TYPE) {
530 sceneType_ = value;
531 } else if (tag == EXIF_TAG_COMPRESSED_BITS_PER_PIXEL) {
532 compressedBitsPerPixel_ = value;
533 } else {
534 SetExifTagValuesEx(tag, value);
535 }
536 }
537
SetExifTagValuesEx(const ExifTag & tag,const std::string & value)538 void EXIFInfo::SetExifTagValuesEx(const ExifTag &tag, const std::string &value)
539 {
540 if (tag == EXIF_TAG_DATE_TIME) {
541 dateTime_ = value;
542 } else if (tag == EXIF_TAG_GPS_TIME_STAMP) {
543 gpsTimeStamp_ = value;
544 } else if (tag == EXIF_TAG_GPS_DATE_STAMP) {
545 gpsDateStamp_ = value;
546 } else if (tag == EXIF_TAG_IMAGE_DESCRIPTION) {
547 imageDescription_ = value;
548 } else if (tag == EXIF_TAG_MAKE) {
549 make_ = value;
550 } else if (tag == EXIF_TAG_MODEL) {
551 model_ = value;
552 } else if (tag == EXIF_TAG_JPEG_PROC) {
553 photoMode_ = value;
554 } else if (tag == TAG_SENSITIVITY_TYPE) {
555 sensitivityType_ = value;
556 } else if (tag == TAG_STANDARD_OUTPUT_SENSITIVITY) {
557 standardOutputSensitivity_ = value;
558 } else if (tag == TAG_RECOMMENDED_EXPOSURE_INDEX) {
559 recommendedExposureIndex_ = value;
560 } else if (tag == EXIF_TAG_APERTURE_VALUE) {
561 apertureValue_ = value;
562 } else if (tag == EXIF_TAG_EXPOSURE_BIAS_VALUE) {
563 exposureBiasValue_ = value;
564 } else if (tag == EXIF_TAG_METERING_MODE) {
565 meteringMode_ = value;
566 } else if (tag == EXIF_TAG_LIGHT_SOURCE) {
567 lightSource_ = value;
568 } else if (tag == EXIF_TAG_FLASH) {
569 flash_ = value;
570 } else if (tag == EXIF_TAG_FOCAL_LENGTH) {
571 focalLength_ = value;
572 } else if (tag == EXIF_TAG_USER_COMMENT) {
573 userComment_ = value;
574 } else if (tag == EXIF_TAG_PIXEL_X_DIMENSION) {
575 pixelXDimension_ = value;
576 } else if (tag == EXIF_TAG_PIXEL_Y_DIMENSION) {
577 pixelYDimension_ = value;
578 } else if (tag == EXIF_TAG_WHITE_BALANCE) {
579 whiteBalance_ = value;
580 } else if (tag == EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM) {
581 focalLengthIn35mmFilm_ = value;
582 } else {
583 IMAGE_LOGD("No match tag name!");
584 }
585 }
586
GetFileInfoByPath(const std::string & path,FILE ** file,unsigned long & fileLength,unsigned char ** fileBuf)587 uint32_t EXIFInfo::GetFileInfoByPath(const std::string &path, FILE **file, unsigned long &fileLength,
588 unsigned char **fileBuf)
589 {
590 *file = fopen(path.c_str(), "rb");
591 if (*file == nullptr) {
592 IMAGE_LOGD("Error creating file %{public}s", path.c_str());
593 return Media::ERR_MEDIA_IO_ABNORMAL;
594 }
595
596 // read jpeg file to buff
597 fileLength = GetFileSize(*file);
598 if (fileLength == 0 || fileLength > MAX_FILE_SIZE) {
599 IMAGE_LOGD("Get file size failed.");
600 (void)fclose(*file);
601 return Media::ERR_MEDIA_BUFFER_TOO_SMALL;
602 }
603
604 *fileBuf = static_cast<unsigned char *>(malloc(fileLength));
605 if (*fileBuf == nullptr) {
606 IMAGE_LOGD("Allocate buf for %{public}s failed.", path.c_str());
607 (void)fclose(*file);
608 return Media::ERR_IMAGE_MALLOC_ABNORMAL;
609 }
610
611 if (fread(*fileBuf, fileLength, 1, *file) != 1) {
612 IMAGE_LOGD("Read %{public}s failed.", path.c_str());
613 ReleaseSource(fileBuf, file);
614 return Media::ERR_MEDIA_READ_PARCEL_FAIL;
615 }
616
617 if (!((*fileBuf)[0] == 0xFF && (*fileBuf)[1] == 0xD8)) {
618 IMAGE_LOGD("%{public}s is not jpeg file.", path.c_str());
619 ReleaseSource(fileBuf, file);
620 return Media::ERR_IMAGE_MISMATCHED_FORMAT;
621 }
622 return Media::SUCCESS;
623 }
624
ModifyExifData(const ExifTag & tag,const std::string & value,const std::string & path)625 uint32_t EXIFInfo::ModifyExifData(const ExifTag &tag, const std::string &value, const std::string &path)
626 {
627 FILE *file = nullptr;
628 unsigned long fileLength;
629 unsigned char *fileBuf = nullptr;
630 uint32_t res = GetFileInfoByPath(path, &file, fileLength, &fileBuf);
631 if (res != Media::SUCCESS) {
632 return res;
633 }
634
635 ExifData *ptrExifData = nullptr;
636 bool isNewExifData = false;
637 if (!CreateExifData(fileBuf, fileLength, &ptrExifData, isNewExifData)) {
638 ReleaseSource(&fileBuf, &file);
639 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
640 }
641 (void)fclose(file);
642 file = nullptr;
643
644 unsigned int orginExifDataLength = GetOrginExifDataLength(isNewExifData, fileBuf);
645 if (!isNewExifData && orginExifDataLength == 0) {
646 IMAGE_LOGD("There is no orginExifDataLength node in %{public}s.", path.c_str());
647 exif_data_unref(ptrExifData);
648 free(fileBuf);
649 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
650 }
651
652 ExifByteOrder order = GetExifByteOrder(isNewExifData, fileBuf);
653 FILE *newFile = fopen(path.c_str(), "wb+");
654 if (newFile == nullptr) {
655 IMAGE_LOGD("Error create new file %{public}s", path.c_str());
656 ReleaseSource(&fileBuf, &newFile);
657 return Media::ERR_MEDIA_IO_ABNORMAL;
658 }
659 ExifEntry *entry = nullptr;
660 if (!CreateExifEntry(tag, ptrExifData, value, order, &entry)) {
661 ReleaseSource(&fileBuf, &newFile);
662 exif_data_unref(ptrExifData);
663 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
664 }
665 if (!WriteExifDataToFile(ptrExifData, orginExifDataLength, fileLength, fileBuf, newFile)) {
666 ReleaseSource(&fileBuf, &newFile);
667 exif_data_unref(ptrExifData);
668 return Media::ERR_MEDIA_WRITE_PARCEL_FAIL;
669 }
670 ReleaseSource(&fileBuf, &newFile);
671 exif_data_unref(ptrExifData);
672 return Media::SUCCESS;
673 }
674
GetFileInfoByFd(int localFd,FILE ** file,unsigned long & fileLength,unsigned char ** fileBuf)675 uint32_t EXIFInfo::GetFileInfoByFd(int localFd, FILE **file, unsigned long &fileLength, unsigned char **fileBuf)
676 {
677 *file = fdopen(localFd, "wb+");
678 if (*file == nullptr) {
679 IMAGE_LOGD("Error creating file %{public}d", localFd);
680 return Media::ERR_MEDIA_IO_ABNORMAL;
681 }
682
683 // read jpeg file to buff
684 fileLength = GetFileSize(*file);
685 if (fileLength == 0 || fileLength > MAX_FILE_SIZE) {
686 IMAGE_LOGD("Get file size failed.");
687 (void)fclose(*file);
688 return Media::ERR_MEDIA_BUFFER_TOO_SMALL;
689 }
690
691 *fileBuf = static_cast<unsigned char *>(malloc(fileLength));
692 if (*fileBuf == nullptr) {
693 IMAGE_LOGD("Allocate buf for %{public}d failed.", localFd);
694 (void)fclose(*file);
695 return Media::ERR_IMAGE_MALLOC_ABNORMAL;
696 }
697
698 // Set current position to begin of file.
699 (void)fseek(*file, 0L, 0);
700 if (fread(*fileBuf, fileLength, 1, *file) != 1) {
701 IMAGE_LOGD("Read %{public}d failed.", localFd);
702 ReleaseSource(fileBuf, file);
703 return Media::ERR_MEDIA_READ_PARCEL_FAIL;
704 }
705
706 if (!((*fileBuf)[0] == 0xFF && (*fileBuf)[1] == 0xD8)) {
707 IMAGE_LOGD("%{public}d is not jpeg file.", localFd);
708 ReleaseSource(fileBuf, file);
709 return Media::ERR_IMAGE_MISMATCHED_FORMAT;
710 }
711 return Media::SUCCESS;
712 }
713
ModifyExifData(const ExifTag & tag,const std::string & value,const int fd)714 uint32_t EXIFInfo::ModifyExifData(const ExifTag &tag, const std::string &value, const int fd)
715 {
716 const int localFd = dup(fd);
717 FILE *file = nullptr;
718 unsigned long fileLength;
719 unsigned char *fileBuf = nullptr;
720 uint32_t res = GetFileInfoByFd(localFd, &file, fileLength, &fileBuf);
721 if (res != Media::SUCCESS) {
722 return res;
723 }
724
725 ExifData *ptrExifData = nullptr;
726 bool isNewExifData = false;
727 if (!CreateExifData(fileBuf, fileLength, &ptrExifData, isNewExifData)) {
728 ReleaseSource(&fileBuf, &file);
729 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
730 }
731
732 unsigned int orginExifDataLength = GetOrginExifDataLength(isNewExifData, fileBuf);
733 if (!isNewExifData && orginExifDataLength == 0) {
734 IMAGE_LOGD("There is no orginExifDataLength node in %{public}d.", localFd);
735 free(fileBuf);
736 exif_data_unref(ptrExifData);
737 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
738 }
739
740 ExifByteOrder order = GetExifByteOrder(isNewExifData, fileBuf);
741 // Set current position to begin of new file.
742 (void)fseek(file, 0L, 0);
743 ExifEntry *entry = nullptr;
744 if (!CreateExifEntry(tag, ptrExifData, value, order, &entry)) {
745 ReleaseSource(&fileBuf, &file);
746 exif_data_unref(ptrExifData);
747 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
748 }
749
750 if (!WriteExifDataToFile(ptrExifData, orginExifDataLength, fileLength, fileBuf, file)) {
751 ReleaseSource(&fileBuf, &file);
752 exif_data_unref(ptrExifData);
753 return Media::ERR_MEDIA_WRITE_PARCEL_FAIL;
754 }
755 ReleaseSource(&fileBuf, &file);
756 exif_data_unref(ptrExifData);
757 return Media::SUCCESS;
758 }
759
ReleaseExifDataBuffer(unsigned char * exifDataBuf)760 void ReleaseExifDataBuffer(unsigned char* exifDataBuf)
761 {
762 if (exifDataBuf != nullptr) {
763 free(exifDataBuf);
764 exifDataBuf = nullptr;
765 }
766 }
767
CheckInputDataValid(unsigned char * data,uint32_t size)768 uint32_t EXIFInfo::CheckInputDataValid(unsigned char *data, uint32_t size)
769 {
770 if (data == nullptr) {
771 IMAGE_LOGD("buffer is nullptr.");
772 return Media::ERR_IMAGE_SOURCE_DATA;
773 }
774
775 if (size == 0) {
776 IMAGE_LOGD("buffer size is 0.");
777 return Media::ERR_MEDIA_BUFFER_TOO_SMALL;
778 }
779
780 if (!(data[0] == 0xFF && data[1] == 0xD8)) {
781 IMAGE_LOGD("This is not jpeg file.");
782 return Media::ERR_IMAGE_MISMATCHED_FORMAT;
783 }
784 return Media::SUCCESS;
785 }
786
ModifyExifData(const ExifTag & tag,const std::string & value,unsigned char * data,uint32_t size)787 uint32_t EXIFInfo::ModifyExifData(const ExifTag &tag, const std::string &value,
788 unsigned char *data, uint32_t size)
789 {
790 uint32_t checkInputRes = CheckInputDataValid(data, size);
791 if (checkInputRes != Media::SUCCESS) {
792 return checkInputRes;
793 }
794
795 ExifData *ptrExifData = nullptr;
796 bool isNewExifData = false;
797 if (!CreateExifData(data, size, &ptrExifData, isNewExifData)) {
798 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
799 }
800
801 unsigned int orginExifDataLength = GetOrginExifDataLength(isNewExifData, data);
802 if (!isNewExifData && orginExifDataLength == 0) {
803 IMAGE_LOGD("There is no orginExifDataLength node in buffer.");
804 exif_data_unref(ptrExifData);
805 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
806 }
807
808 ExifByteOrder order = GetExifByteOrder(isNewExifData, data);
809 ExifEntry *entry = nullptr;
810 if (!CreateExifEntry(tag, ptrExifData, value, order, &entry)) {
811 exif_data_unref(ptrExifData);
812 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
813 }
814
815 unsigned char* exifDataBuf = nullptr;
816 unsigned int exifDataBufLength = 0;
817 exif_data_save_data(ptrExifData, &exifDataBuf, &exifDataBufLength);
818 if (exifDataBuf == nullptr) {
819 IMAGE_LOGD("Get Exif Data Buf failed!");
820 exif_data_unref(ptrExifData);
821 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
822 }
823
824 if (size == 0 || size > MAX_FILE_SIZE) {
825 IMAGE_LOGD("Buffer size is out of range.");
826 exif_data_unref(ptrExifData);
827 ReleaseExifDataBuffer(exifDataBuf);
828 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
829 }
830 unsigned char *tempBuf = static_cast<unsigned char *>(malloc(size));
831 if (tempBuf == nullptr) {
832 IMAGE_LOGD("Allocate temp buffer ailed.");
833 exif_data_unref(ptrExifData);
834 ReleaseExifDataBuffer(exifDataBuf);
835 return Media::ERR_IMAGE_MALLOC_ABNORMAL;
836 }
837
838 // Write EXIF header to buffer
839 uint32_t index = 0;
840 if (sizeof(EXIF_HEADER) >= size) {
841 IMAGE_LOGD("There is not enough space for EXIF header!");
842 ReleaseExifData(tempBuf, ptrExifData, exifDataBuf);
843 return Media::ERR_MEDIA_OUT_OF_RANGE;
844 }
845
846 for (size_t i = 0; i < sizeof(EXIF_HEADER); i++) {
847 tempBuf[index++] = EXIF_HEADER[i];
848 }
849
850 // Write EXIF block length in big-endian order
851 unsigned char highBit = static_cast<unsigned char>((exifDataBufLength + LENGTH_OFFSET_2) >> MOVE_OFFSET_8);
852 if (index >= size) {
853 IMAGE_LOGD("There is not enough space for writing EXIF block length!");
854 ReleaseExifData(tempBuf, ptrExifData, exifDataBuf);
855 return Media::ERR_MEDIA_OUT_OF_RANGE;
856 }
857 tempBuf[index++] = highBit;
858
859 unsigned char lowBit = static_cast<unsigned char>((exifDataBufLength + LENGTH_OFFSET_2) & 0xff);
860 if (index >= size) {
861 IMAGE_LOGD("There is not enough space for writing EXIF block length!");
862 ReleaseExifData(tempBuf, ptrExifData, exifDataBuf);
863 return Media::ERR_MEDIA_OUT_OF_RANGE;
864 }
865 tempBuf[index++] = lowBit;
866
867 // Write EXIF data block
868 if ((index + exifDataBufLength) >= size) {
869 IMAGE_LOGD("There is not enough space for writing EXIF data block!");
870 ReleaseExifData(tempBuf, ptrExifData, exifDataBuf);
871 return Media::ERR_MEDIA_OUT_OF_RANGE;
872 }
873 for (unsigned int i = 0; i < exifDataBufLength; i++) {
874 tempBuf[index++] = exifDataBuf[i];
875 }
876
877 // Write JPEG image data, skipping the non-EXIF header
878 if ((index + size - orginExifDataLength - sizeof(EXIF_HEADER) - MOVE_OFFSET_8) > size) {
879 IMAGE_LOGD("There is not enough space for writing JPEG image data!");
880 ReleaseExifData(tempBuf, ptrExifData, exifDataBuf);
881 return Media::ERR_MEDIA_OUT_OF_RANGE;
882 }
883
884 for (unsigned int i = 0; i < (size - orginExifDataLength - sizeof(EXIF_HEADER)); i++) {
885 tempBuf[index++] = data[orginExifDataLength + sizeof(EXIF_HEADER) + i];
886 }
887
888 for (unsigned int i = 0; i < size; i++) {
889 data[i] = tempBuf[i];
890 }
891
892 ParseExifData(data, static_cast<unsigned int>(index));
893 ReleaseExifData(tempBuf, ptrExifData, exifDataBuf);
894 return Media::SUCCESS;
895 }
896
ReleaseExifData(unsigned char * tempBuf,ExifData * ptrExifData,unsigned char * exifDataBuf)897 void EXIFInfo::ReleaseExifData(unsigned char *tempBuf, ExifData *ptrExifData, unsigned char* exifDataBuf)
898 {
899 if (tempBuf != nullptr) {
900 free(tempBuf);
901 tempBuf = nullptr;
902 }
903 exif_data_unref(ptrExifData);
904 ReleaseExifDataBuffer(exifDataBuf);
905 }
906
InitExifTag(ExifData * exif,ExifIfd ifd,ExifTag tag)907 ExifEntry* EXIFInfo::InitExifTag(ExifData *exif, ExifIfd ifd, ExifTag tag)
908 {
909 ExifEntry *entry;
910 /* Return an existing tag if one exists */
911 if (!(entry = exif_content_get_entry(exif->ifd[ifd], tag))) {
912 /* Allocate a new entry */
913 entry = exif_entry_new();
914 if (entry == nullptr) {
915 IMAGE_LOGD("Create new entry failed!");
916 return nullptr;
917 }
918 entry->tag = tag; // tag must be set before calling exif_content_add_entry
919 /* Attach the ExifEntry to an IFD */
920 exif_content_add_entry (exif->ifd[ifd], entry);
921
922 /* Allocate memory for the entry and fill with default data */
923 exif_entry_initialize (entry, tag);
924
925 /* Ownership of the ExifEntry has now been passed to the IFD.
926 * One must be very careful in accessing a structure after
927 * unref'ing it; in this case, we know "entry" won't be freed
928 * because the reference count was bumped when it was added to
929 * the IFD.
930 */
931 exif_entry_unref(entry);
932 }
933 return entry;
934 }
EXIFInfoBufferCheck(ExifEntry * exifEntry,size_t len)935 static void EXIFInfoBufferCheck(ExifEntry *exifEntry, size_t len)
936 {
937 if (exifEntry == nullptr || (exifEntry->size >= len)) {
938 return;
939 }
940 /* Create a memory allocator to manage this ExifEntry */
941 ExifMem *exifMem = exif_mem_new_default();
942 if (exifMem == nullptr) {
943 IMAGE_LOGD("Create mem failed!");
944 return;
945 }
946 auto buf = exif_mem_realloc(exifMem, exifEntry->data, len);
947 if (buf != nullptr) {
948 exifEntry->data = static_cast<unsigned char*>(buf);
949 exifEntry->size = len;
950 }
951 exif_mem_unref(exifMem);
952 }
953
CreateExifTag(ExifData * exif,ExifIfd ifd,ExifTag tag,size_t len,ExifFormat format)954 ExifEntry* EXIFInfo::CreateExifTag(ExifData *exif, ExifIfd ifd, ExifTag tag,
955 size_t len, ExifFormat format)
956 {
957 void *buf;
958 ExifEntry *exifEntry;
959
960 if ((exifEntry = exif_content_get_entry(exif->ifd[ifd], tag)) != nullptr) {
961 EXIFInfoBufferCheck(exifEntry, len);
962 return exifEntry;
963 }
964
965 /* Create a memory allocator to manage this ExifEntry */
966 ExifMem *exifMem = exif_mem_new_default();
967 if (exifMem == nullptr) {
968 IMAGE_LOGD("Create mem failed!");
969 return nullptr;
970 }
971
972 /* Create a new ExifEntry using our allocator */
973 exifEntry = exif_entry_new_mem (exifMem);
974 if (exifEntry == nullptr) {
975 IMAGE_LOGD("Create entry by mem failed!");
976 return nullptr;
977 }
978
979 /* Allocate memory to use for holding the tag data */
980 buf = exif_mem_alloc(exifMem, len);
981 if (buf == nullptr) {
982 IMAGE_LOGD("Allocate memory failed!");
983 return nullptr;
984 }
985
986 /* Fill in the entry */
987 exifEntry->data = static_cast<unsigned char*>(buf);
988 exifEntry->size = len;
989 exifEntry->tag = tag;
990 exifEntry->components = len;
991 exifEntry->format = format;
992
993 /* Attach the ExifEntry to an IFD */
994 exif_content_add_entry (exif->ifd[ifd], exifEntry);
995
996 /* The ExifMem and ExifEntry are now owned elsewhere */
997 exif_mem_unref(exifMem);
998 exif_entry_unref(exifEntry);
999
1000 return exifEntry;
1001 }
1002
GetExifTag(ExifData * exif,ExifIfd ifd,ExifTag tag,size_t len)1003 ExifEntry* EXIFInfo::GetExifTag(ExifData *exif, ExifIfd ifd, ExifTag tag, size_t len)
1004 {
1005 ExifEntry *exifEntry;
1006 if ((exifEntry = exif_content_get_entry(exif->ifd[ifd], tag)) != nullptr) {
1007 EXIFInfoBufferCheck(exifEntry, len);
1008 return exifEntry;
1009 }
1010 return nullptr;
1011 }
1012
GetFileSize(FILE * fp)1013 unsigned long EXIFInfo::GetFileSize(FILE *fp)
1014 {
1015 long int position;
1016 long size;
1017
1018 /* Save the current position. */
1019 position = ftell(fp);
1020
1021 /* Jump to the end of the file. */
1022 (void)fseek(fp, 0L, SEEK_END);
1023
1024 /* Get the end position. */
1025 size = ftell(fp);
1026
1027 /* Jump back to the original position. */
1028 (void)fseek(fp, position, SEEK_SET);
1029
1030 return static_cast<unsigned long>(size);
1031 }
1032
CreateExifData(unsigned char * buf,unsigned long length,ExifData ** ptrData,bool & isNewExifData)1033 bool EXIFInfo::CreateExifData(unsigned char *buf, unsigned long length, ExifData **ptrData, bool &isNewExifData)
1034 {
1035 if ((buf[BUFFER_POSITION_6] == 'E' && buf[BUFFER_POSITION_7] == 'x' &&
1036 buf[BUFFER_POSITION_8] == 'i' && buf[BUFFER_POSITION_9] == 'f')) {
1037 *ptrData = exif_data_new_from_data(buf, static_cast<unsigned int>(length));
1038 if (!(*ptrData)) {
1039 IMAGE_LOGD("Create exif data from file failed.");
1040 return false;
1041 }
1042 isNewExifData = false;
1043 IMAGE_LOGD("Create exif data from buffer.");
1044 } else {
1045 *ptrData = exif_data_new();
1046 if (!(*ptrData)) {
1047 IMAGE_LOGD("Create exif data failed.");
1048 return false;
1049 }
1050 /* Set the image options */
1051 exif_data_set_option(*ptrData, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
1052 exif_data_set_data_type(*ptrData, EXIF_DATA_TYPE_COMPRESSED);
1053 exif_data_set_byte_order(*ptrData, EXIF_BYTE_ORDER_INTEL);
1054
1055 /* Create the mandatory EXIF fields with default data */
1056 exif_data_fix(*ptrData);
1057 isNewExifData = true;
1058 IMAGE_LOGD("Create new exif data.");
1059 }
1060 return true;
1061 }
1062
GetOrginExifDataLength(const bool & isNewExifData,unsigned char * buf)1063 unsigned int EXIFInfo::GetOrginExifDataLength(const bool &isNewExifData, unsigned char *buf)
1064 {
1065 unsigned int orginExifDataLength = 0;
1066 if (!isNewExifData) {
1067 orginExifDataLength = static_cast<unsigned int>(buf[BUFFER_POSITION_5]) |
1068 static_cast<unsigned int>(buf[BUFFER_POSITION_4] << MOVE_OFFSET_8);
1069 }
1070 return orginExifDataLength;
1071 }
1072
GetExifByteOrder(const bool & isNewExifData,unsigned char * buf)1073 ExifByteOrder EXIFInfo::GetExifByteOrder(const bool &isNewExifData, unsigned char *buf)
1074 {
1075 if (isNewExifData) {
1076 return EXIF_BYTE_ORDER_INTEL;
1077 } else {
1078 if (buf[BUFFER_POSITION_12] == 'M' && buf[BUFFER_POSITION_13] == 'M') {
1079 return EXIF_BYTE_ORDER_MOTOROLA;
1080 } else {
1081 return EXIF_BYTE_ORDER_INTEL;
1082 }
1083 }
1084 }
1085
GCD(int a,int b)1086 static int GCD(int a, int b)
1087 {
1088 if (b == 0) {
1089 return a;
1090 }
1091 return GCD(b, a % b);
1092 }
1093
GetFractionFromStr(const std::string & decimal,ExifRational & result)1094 static bool GetFractionFromStr(const std::string &decimal, ExifRational &result)
1095 {
1096 int intPart = stoi(decimal.substr(0, decimal.find(".")));
1097 double decPart = stod(decimal.substr(decimal.find(".")));
1098
1099 int numerator = decPart * pow(10, decimal.length() - decimal.find(".") - 1);
1100 int denominator = pow(10, decimal.length() - decimal.find(".") - 1);
1101
1102 int gcdVal = GCD(numerator, denominator);
1103 if (gcdVal == 0) {
1104 IMAGE_LOGD("gcdVal is zero");
1105 return false;
1106 }
1107 numerator /= gcdVal;
1108 denominator /= gcdVal;
1109
1110 numerator += intPart * denominator;
1111
1112 result.numerator = numerator;
1113 result.denominator = denominator;
1114 return true;
1115 }
1116
ExifIntValueByFormat(unsigned char * b,ExifByteOrder order,ExifFormat format,long value)1117 static void ExifIntValueByFormat(unsigned char *b, ExifByteOrder order, ExifFormat format, long value)
1118 {
1119 switch (format) {
1120 case EXIF_FORMAT_SHORT:
1121 exif_set_short(b, order, (ExifShort)value);
1122 break;
1123 case EXIF_FORMAT_SSHORT:
1124 exif_set_sshort(b, order, (ExifSShort)value);
1125 break;
1126 case EXIF_FORMAT_LONG:
1127 exif_set_long(b, order, (ExifLong)value);
1128 break;
1129 case EXIF_FORMAT_SLONG:
1130 exif_set_slong(b, order, (ExifSLong)value);
1131 break;
1132 case EXIF_FORMAT_BYTE:
1133 case EXIF_FORMAT_SRATIONAL:
1134 case EXIF_FORMAT_UNDEFINED:
1135 case EXIF_FORMAT_ASCII:
1136 case EXIF_FORMAT_RATIONAL:
1137 default:
1138 IMAGE_LOGD("ExifIntValueByFormat unsupported format %{public}d.", format);
1139 break;
1140 }
1141 }
1142
ConvertStringToDouble(const std::string & str,double & number)1143 static bool ConvertStringToDouble(const std::string &str, double &number)
1144 {
1145 char* end = nullptr;
1146 number = std::strtod(str.c_str(), &end);
1147 return end != str.c_str() && *end == '\0' && number != HUGE_VAL;
1148 }
1149
IsValidGpsData(const std::vector<std::string> & dataVec,const ExifTag & tag)1150 static bool IsValidGpsData(const std::vector<std::string> &dataVec, const ExifTag &tag)
1151 {
1152 if (dataVec.size() != SIZE_3 || (tag != EXIF_TAG_GPS_LATITUDE && tag != EXIF_TAG_GPS_LONGITUDE)) {
1153 IMAGE_LOGD("Gps dms data size is invalid.");
1154 return false;
1155 }
1156 double degree = 0.0;
1157 double minute = 0.0;
1158 double second = 0.0;
1159 if (!ConvertStringToDouble(dataVec[CONSTANT_0], degree) ||
1160 !ConvertStringToDouble(dataVec[CONSTANT_1], minute) ||
1161 !ConvertStringToDouble(dataVec[CONSTANT_2], second)) {
1162 IMAGE_LOGE("Convert gps data to double type failed.");
1163 return false;
1164 }
1165 constexpr uint32_t timePeriod = 60;
1166 double latOrLong = degree + minute / timePeriod + second / (timePeriod * timePeriod);
1167 if ((tag == EXIF_TAG_GPS_LATITUDE && (latOrLong > GPS_MAX_LATITUDE || latOrLong < GPS_MIN_LATITUDE)) ||
1168 (tag == EXIF_TAG_GPS_LONGITUDE && (latOrLong > GPS_MAX_LONGITUDE || latOrLong < GPS_MIN_LONGITUDE))) {
1169 IMAGE_LOGD("Gps latitude or longitude is out of range.");
1170 return false;
1171 }
1172 return true;
1173 }
1174
ConvertGpsDataToRationals(const std::vector<std::string> & dataVec,std::vector<ExifRational> & exifRationals)1175 static bool ConvertGpsDataToRationals(const std::vector<std::string> &dataVec,
1176 std::vector<ExifRational> &exifRationals)
1177 {
1178 if (!(dataVec.size() == SIZE_3 && exifRationals.size() == SIZE_3)) {
1179 IMAGE_LOGD("Data size is invalid.");
1180 return false;
1181 }
1182
1183 int32_t degree = static_cast<int32_t>(atoi(dataVec[CONSTANT_0].c_str()));
1184 int32_t minute = static_cast<int32_t>(atoi(dataVec[CONSTANT_1].c_str()));
1185 double secondDouble = 0.0;
1186 ConvertStringToDouble(dataVec[CONSTANT_2], secondDouble);
1187 int32_t second = static_cast<int32_t>(secondDouble * GPS_DIGIT_NUMBER);
1188
1189 exifRationals[CONSTANT_0].numerator = static_cast<ExifSLong>(degree);
1190 exifRationals[CONSTANT_0].denominator = static_cast<ExifSLong>(1);
1191 exifRationals[CONSTANT_1].numerator = static_cast<ExifSLong>(minute);
1192 exifRationals[CONSTANT_1].denominator = static_cast<ExifSLong>(1);
1193 exifRationals[CONSTANT_2].numerator = static_cast<ExifSLong>(second);
1194 exifRationals[CONSTANT_2].denominator = static_cast<ExifSLong>(GPS_DIGIT_NUMBER);
1195 return true;
1196 }
1197
SetGpsRationals(ExifData * data,ExifEntry ** ptrEntry,ExifByteOrder order,const ExifTag & tag,const std::vector<ExifRational> & exifRationals)1198 bool EXIFInfo::SetGpsRationals(ExifData *data, ExifEntry **ptrEntry, ExifByteOrder order,
1199 const ExifTag &tag, const std::vector<ExifRational> &exifRationals)
1200 {
1201 if (exifRationals.size() != SIZE_3) {
1202 IMAGE_LOGD("ExifRationals size is invalid.");
1203 return false;
1204 }
1205 *ptrEntry = GetExifTag(data, EXIF_IFD_GPS, tag, MOVE_OFFSET_24);
1206 if ((*ptrEntry) == nullptr) {
1207 IMAGE_LOGD("Get exif entry failed.");
1208 return false;
1209 }
1210 exif_set_rational((*ptrEntry)->data, order, exifRationals[CONSTANT_0]);
1211 exif_set_rational((*ptrEntry)->data + MOVE_OFFSET_8, order, exifRationals[CONSTANT_1]);
1212 exif_set_rational((*ptrEntry)->data + MOVE_OFFSET_16, order, exifRationals[CONSTANT_2]);
1213 return true;
1214 }
1215
SetGpsDegreeRational(ExifData * data,ExifEntry ** ptrEntry,ExifByteOrder order,const ExifTag & tag,const std::vector<std::string> & dataVec)1216 bool EXIFInfo::SetGpsDegreeRational(ExifData *data, ExifEntry **ptrEntry, ExifByteOrder order, const ExifTag &tag,
1217 const std::vector<std::string> &dataVec)
1218 {
1219 if (dataVec.size() != SIZE_2) {
1220 IMAGE_LOGD("Gps degree data size is invalid.");
1221 return false;
1222 }
1223 ExifRational exifRational;
1224 exifRational.numerator = static_cast<ExifSLong>(atoi(dataVec[CONSTANT_0].c_str()));
1225 exifRational.denominator = static_cast<ExifSLong>(atoi(dataVec[CONSTANT_1].c_str()));
1226 *ptrEntry = CreateExifTag(data, EXIF_IFD_GPS, tag, MOVE_OFFSET_8, EXIF_FORMAT_RATIONAL);
1227 if ((*ptrEntry) == nullptr) {
1228 IMAGE_LOGD("Get exif entry failed.");
1229 return false;
1230 }
1231 exif_set_rational((*ptrEntry)->data, order, exifRational);
1232 return true;
1233 }
1234
CreateExifEntryOfBitsPerSample(const ExifTag & tag,ExifData * data,const std::string & value,ExifByteOrder order,ExifEntry ** ptrEntry)1235 bool EXIFInfo::CreateExifEntryOfBitsPerSample(const ExifTag &tag, ExifData *data, const std::string &value,
1236 ExifByteOrder order, ExifEntry **ptrEntry)
1237 {
1238 *ptrEntry = InitExifTag(data, EXIF_IFD_0, EXIF_TAG_BITS_PER_SAMPLE);
1239 if ((*ptrEntry) == nullptr) {
1240 IMAGE_LOGD("Get exif entry failed.");
1241 return false;
1242 }
1243 std::vector<std::string> bitsVec;
1244 SplitStr(value, ",", bitsVec);
1245 if (bitsVec.size() > CONSTANT_4) {
1246 IMAGE_LOGD("BITS_PER_SAMPLE Invalid value %{public}s", value.c_str());
1247 return false;
1248 }
1249 if (bitsVec.size() != 0) {
1250 for (size_t i = 0; i < bitsVec.size(); i++) {
1251 exif_set_short((*ptrEntry)->data + i * CONSTANT_2, order, (ExifShort)atoi(bitsVec[i].c_str()));
1252 }
1253 }
1254 return true;
1255 }
1256
GetExifIfdByExifTag(const ExifTag & tag)1257 ExifIfd EXIFInfo::GetExifIfdByExifTag(const ExifTag &tag)
1258 {
1259 static std::vector exifIfd0Vec{EXIF_TAG_ORIENTATION, EXIF_TAG_IMAGE_LENGTH, EXIF_TAG_IMAGE_WIDTH,
1260 EXIF_TAG_IMAGE_DESCRIPTION, EXIF_TAG_MAKE, EXIF_TAG_MODEL, EXIF_TAG_DATE_TIME};
1261 static std::vector exifIfdExifVec{EXIF_TAG_WHITE_BALANCE, EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM, EXIF_TAG_FLASH,
1262 EXIF_TAG_ISO_SPEED_RATINGS, EXIF_TAG_ISO_SPEED, EXIF_TAG_LIGHT_SOURCE, EXIF_TAG_METERING_MODE,
1263 EXIF_TAG_PIXEL_X_DIMENSION, EXIF_TAG_PIXEL_Y_DIMENSION, EXIF_TAG_RECOMMENDED_EXPOSURE_INDEX,
1264 EXIF_TAG_SENSITIVITY_TYPE, EXIF_TAG_SCENE_TYPE, EXIF_TAG_STANDARD_OUTPUT_SENSITIVITY, EXIF_TAG_USER_COMMENT,
1265 EXIF_TAG_DATE_TIME_ORIGINAL, EXIF_TAG_APERTURE_VALUE, EXIF_TAG_EXPOSURE_BIAS_VALUE, EXIF_TAG_EXPOSURE_TIME,
1266 EXIF_TAG_FNUMBER, EXIF_TAG_FOCAL_LENGTH};
1267 static std::vector exifIfdGpsVec{EXIF_TAG_GPS_DATE_STAMP, EXIF_TAG_GPS_LATITUDE_REF, EXIF_TAG_GPS_LONGITUDE_REF,
1268 EXIF_TAG_GPS_LATITUDE, EXIF_TAG_GPS_LONGITUDE};
1269 if (std::find(exifIfd0Vec.begin(), exifIfd0Vec.end(), tag) != exifIfd0Vec.end()) {
1270 return EXIF_IFD_0;
1271 } else if (std::find(exifIfdExifVec.begin(), exifIfdExifVec.end(), tag) != exifIfdExifVec.end()) {
1272 return EXIF_IFD_EXIF;
1273 } else if (std::find(exifIfdGpsVec.begin(), exifIfdGpsVec.end(), tag) != exifIfdGpsVec.end()) {
1274 return EXIF_IFD_GPS;
1275 }
1276 return EXIF_IFD_COUNT;
1277 }
1278
GetExifFormatByExifTag(const ExifTag & tag)1279 ExifFormat EXIFInfo::GetExifFormatByExifTag(const ExifTag &tag)
1280 {
1281 static std::vector exifFormatAscii{EXIF_TAG_GPS_DATE_STAMP, EXIF_TAG_IMAGE_DESCRIPTION, EXIF_TAG_MAKE,
1282 EXIF_TAG_MODEL, EXIF_TAG_DATE_TIME_ORIGINAL, EXIF_TAG_DATE_TIME, EXIF_TAG_GPS_LATITUDE_REF,
1283 EXIF_TAG_GPS_LONGITUDE_REF};
1284 static std::vector exifFormatRational{EXIF_TAG_GPS_LATITUDE, EXIF_TAG_GPS_LONGITUDE, EXIF_TAG_APERTURE_VALUE};
1285 static std::vector exifFormatSRational{EXIF_TAG_EXPOSURE_BIAS_VALUE, EXIF_TAG_EXPOSURE_TIME, EXIF_TAG_FNUMBER,
1286 EXIF_TAG_FOCAL_LENGTH};
1287 if (std::find(exifFormatAscii.begin(), exifFormatAscii.end(), tag) != exifFormatAscii.end()) {
1288 return EXIF_FORMAT_ASCII;
1289 } else if (std::find(exifFormatRational.begin(), exifFormatRational.end(), tag) != exifFormatRational.end()) {
1290 return EXIF_FORMAT_RATIONAL;
1291 } else if (std::find(exifFormatSRational.begin(), exifFormatSRational.end(), tag) != exifFormatSRational.end()) {
1292 return EXIF_FORMAT_SRATIONAL;
1293 } else if (tag == EXIF_TAG_SCENE_TYPE || tag == EXIF_TAG_USER_COMMENT) {
1294 return EXIF_FORMAT_UNDEFINED;
1295 } else if (tag == EXIF_TAG_STANDARD_OUTPUT_SENSITIVITY) {
1296 return EXIF_FORMAT_LONG;
1297 }
1298 return EXIF_FORMAT_UNDEFINED;
1299 }
1300
GetExifNameByExifTag(const ExifTag & tag)1301 std::string EXIFInfo::GetExifNameByExifTag(const ExifTag &tag)
1302 {
1303 for (uint32_t i = 0; i < sizeof(EXIF_TAG_TABLE) / sizeof(EXIF_TAG_TABLE[0]); i++) {
1304 if (EXIF_TAG_TABLE[i].tag != tag) {
1305 return EXIF_TAG_TABLE[i].name;
1306 }
1307 }
1308 return "";
1309 }
CreateExifEntryOfRationalExif(const ExifTag & tag,ExifData * data,const std::string & value,ExifByteOrder order,ExifEntry ** ptrEntry,const std::string & separator,size_t sepSize)1310 bool EXIFInfo::CreateExifEntryOfRationalExif(const ExifTag &tag, ExifData *data, const std::string &value,
1311 ExifByteOrder order, ExifEntry **ptrEntry, const std::string& separator, size_t sepSize)
1312 {
1313 std::vector<std::string> longVec;
1314 SplitStr(value, separator, longVec);
1315 if (longVec.size() != sepSize) {
1316 IMAGE_LOGD("%{public}s Invalid value %{public}s", GetExifNameByExifTag(tag).c_str(), value.c_str());
1317 return false;
1318 }
1319 ExifRational longRational;
1320 longRational.numerator = static_cast<ExifSLong>(atoi(longVec[0].c_str()));
1321 longRational.denominator = static_cast<ExifSLong>(atoi(longVec[1].c_str()));
1322 *ptrEntry = CreateExifTag(data, GetExifIfdByExifTag(tag), tag, sizeof(longRational), GetExifFormatByExifTag(tag));
1323 if ((*ptrEntry) == nullptr) {
1324 IMAGE_LOGD("Get %{public}s exif entry failed.", GetExifNameByExifTag(tag).c_str());
1325 return false;
1326 }
1327 exif_set_rational((*ptrEntry)->data, order, longRational);
1328 return true;
1329 }
1330
CreateExifEntryOfGpsTimeStamp(const ExifTag & tag,ExifData * data,const std::string & value,ExifByteOrder order,ExifEntry ** ptrEntry)1331 bool EXIFInfo::CreateExifEntryOfGpsTimeStamp(const ExifTag &tag, ExifData *data, const std::string &value,
1332 ExifByteOrder order, ExifEntry **ptrEntry)
1333 {
1334 std::vector<std::string> longVec;
1335 SplitStr(value, ":", longVec);
1336 if (longVec.size() != CONSTANT_3) {
1337 IMAGE_LOGD("GPS time stamp Invalid value %{public}s", value.c_str());
1338 return false;
1339 }
1340 *ptrEntry = CreateExifTag(data, EXIF_IFD_GPS, tag, MOVE_OFFSET_24, EXIF_FORMAT_SRATIONAL);
1341 if ((*ptrEntry) == nullptr) {
1342 IMAGE_LOGD("Get GPS time stamp exif entry failed.");
1343 return false;
1344 }
1345 exif_set_long((*ptrEntry)->data, order, static_cast<ExifSLong>(atoi(longVec[CONSTANT_0].c_str())));
1346 exif_set_long((*ptrEntry)->data + MOVE_OFFSET_8, order,
1347 static_cast<ExifSLong>(atoi(longVec[CONSTANT_1].c_str())));
1348 exif_set_long((*ptrEntry)->data + MOVE_OFFSET_16, order,
1349 static_cast<ExifSLong>(atoi(longVec[CONSTANT_2].c_str())));
1350 return true;
1351 }
1352
CreateExifEntryOfCompressedBitsPerPixel(const ExifTag & tag,ExifData * data,const std::string & value,ExifByteOrder order,ExifEntry ** ptrEntry)1353 bool EXIFInfo::CreateExifEntryOfCompressedBitsPerPixel(const ExifTag &tag, ExifData *data, const std::string &value,
1354 ExifByteOrder order, ExifEntry **ptrEntry)
1355 {
1356 *ptrEntry = InitExifTag(data, EXIF_IFD_EXIF, tag);
1357 if ((*ptrEntry) == nullptr) {
1358 IMAGE_LOGD("Get exif entry failed.");
1359 return false;
1360 }
1361 ExifRational rat;
1362 if (!GetFractionFromStr(value, rat)) {
1363 IMAGE_LOGD("Get fraction from value failed.");
1364 return false;
1365 }
1366 exif_set_rational((*ptrEntry)->data, order, rat);
1367 return true;
1368 }
1369
CreateExifEntryOfGpsLatitudeOrLongitude(const ExifTag & tag,ExifData * data,const std::string & value,ExifByteOrder order,ExifEntry ** ptrEntry)1370 bool EXIFInfo::CreateExifEntryOfGpsLatitudeOrLongitude(const ExifTag &tag, ExifData *data, const std::string &value,
1371 ExifByteOrder order, ExifEntry **ptrEntry)
1372 {
1373 std::vector<std::string> longVec;
1374 SplitStr(value, ",", longVec);
1375 if (longVec.size() == CONSTANT_2) {
1376 return SetGpsDegreeRational(data, ptrEntry, order, tag, longVec);
1377 }
1378 if (longVec.size() != CONSTANT_3 || !IsValidGpsData(longVec, tag)) {
1379 IMAGE_LOGD("%{public}s Invalid value %{public}s", GetExifNameByExifTag(tag).c_str(),
1380 value.c_str());
1381 return false;
1382 }
1383 std::vector<ExifRational> longRational(GPS_DMS_COUNT);
1384 return ConvertGpsDataToRationals(longVec, longRational) &&
1385 SetGpsRationals(data, ptrEntry, order, tag, longRational);
1386 }
1387
CreateExifEntry(const ExifTag & tag,ExifData * data,const std::string & value,ExifByteOrder order,ExifEntry ** ptrEntry)1388 bool EXIFInfo::CreateExifEntry(const ExifTag &tag, ExifData *data, const std::string &value,
1389 ExifByteOrder order, ExifEntry **ptrEntry)
1390 {
1391 static std::vector vector1{EXIF_TAG_ORIENTATION, EXIF_TAG_IMAGE_LENGTH, EXIF_TAG_IMAGE_WIDTH,
1392 EXIF_TAG_WHITE_BALANCE, EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM, EXIF_TAG_FLASH, EXIF_TAG_ISO_SPEED_RATINGS,
1393 EXIF_TAG_ISO_SPEED, EXIF_TAG_LIGHT_SOURCE, EXIF_TAG_METERING_MODE, EXIF_TAG_PIXEL_X_DIMENSION,
1394 EXIF_TAG_PIXEL_Y_DIMENSION, EXIF_TAG_RECOMMENDED_EXPOSURE_INDEX, EXIF_TAG_SENSITIVITY_TYPE};
1395 static std::vector vector2{EXIF_TAG_GPS_DATE_STAMP, EXIF_TAG_IMAGE_DESCRIPTION, EXIF_TAG_MAKE, EXIF_TAG_MODEL,
1396 EXIF_TAG_SCENE_TYPE, EXIF_TAG_STANDARD_OUTPUT_SENSITIVITY, EXIF_TAG_USER_COMMENT, EXIF_TAG_DATE_TIME_ORIGINAL,
1397 EXIF_TAG_DATE_TIME, EXIF_TAG_GPS_LATITUDE_REF, EXIF_TAG_GPS_LONGITUDE_REF};
1398 static std::vector vector3{EXIF_TAG_APERTURE_VALUE, EXIF_TAG_EXPOSURE_BIAS_VALUE, EXIF_TAG_EXPOSURE_TIME,
1399 EXIF_TAG_FNUMBER, EXIF_TAG_FOCAL_LENGTH};
1400 if (tag == EXIF_TAG_BITS_PER_SAMPLE) {
1401 return CreateExifEntryOfBitsPerSample(tag, data, value, order, ptrEntry);
1402 } else if (std::find(vector1.begin(), vector1.end(), tag) != vector1.end()) {
1403 *ptrEntry = InitExifTag(data, GetExifIfdByExifTag(tag), tag);
1404 if ((*ptrEntry) == nullptr) {
1405 IMAGE_LOGD("Get %{public}s exif entry failed.", GetExifNameByExifTag(tag).c_str());
1406 return false;
1407 }
1408 ExifIntValueByFormat((*ptrEntry)->data, order, (*ptrEntry)->format, atoi(value.c_str()));
1409 return true;
1410 } else if (std::find(vector2.begin(), vector2.end(), tag) != vector2.end()) {
1411 *ptrEntry = CreateExifTag(data, GetExifIfdByExifTag(tag), tag, value.length(), GetExifFormatByExifTag(tag));
1412 if ((*ptrEntry) == nullptr || (*ptrEntry)->size < value.length()) {
1413 IMAGE_LOGD("Get %{public}s exif entry failed.", GetExifNameByExifTag(tag).c_str());
1414 return false;
1415 }
1416 if (memcpy_s((*ptrEntry)->data, value.length(), value.c_str(), value.length()) != 0) {
1417 IMAGE_LOGD("%{public}s memcpy error", GetExifNameByExifTag(tag).c_str());
1418 }
1419 return true;
1420 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE) {
1421 return CreateExifEntryOfGpsLatitudeOrLongitude(tag, data, value, order, ptrEntry);
1422 } else if (std::find(vector3.begin(), vector3.end(), tag) != vector3.end()) {
1423 return CreateExifEntryOfRationalExif(tag, data, value, order, ptrEntry, "/", static_cast<size_t>(CONSTANT_2));
1424 } else if (tag == EXIF_TAG_COMPRESSED_BITS_PER_PIXEL) {
1425 return CreateExifEntryOfCompressedBitsPerPixel(tag, data, value, order, ptrEntry);
1426 } else if (tag == EXIF_TAG_GPS_TIME_STAMP) {
1427 return CreateExifEntryOfGpsTimeStamp(tag, data, value, order, ptrEntry);
1428 }
1429 return true;
1430 }
1431
WriteExifDataToFile(ExifData * data,unsigned int orginExifDataLength,unsigned long fileLength,unsigned char * buf,FILE * fp)1432 bool EXIFInfo::WriteExifDataToFile(ExifData *data, unsigned int orginExifDataLength, unsigned long fileLength,
1433 unsigned char *buf, FILE *fp)
1434 {
1435 unsigned char* exifDataBuf = nullptr;
1436 unsigned int exifDataBufLength = 0;
1437 exif_data_save_data(data, &exifDataBuf, &exifDataBufLength);
1438 if (exifDataBuf == nullptr) {
1439 IMAGE_LOGD("Get Exif Data Buf failed!");
1440 return false;
1441 }
1442
1443 // Write EXIF header
1444 if (fwrite(EXIF_HEADER, sizeof(EXIF_HEADER), 1, fp) != 1) {
1445 IMAGE_LOGD("Error writing EXIF header to file!");
1446 ReleaseExifDataBuffer(exifDataBuf);
1447 return false;
1448 }
1449
1450 // Write EXIF block length in big-endian order
1451 if (fputc((exifDataBufLength + LENGTH_OFFSET_2) >> MOVE_OFFSET_8, fp) < 0) {
1452 IMAGE_LOGD("Error writing EXIF block length to file!");
1453 ReleaseExifDataBuffer(exifDataBuf);
1454 return false;
1455 }
1456
1457 if (fputc((exifDataBufLength + LENGTH_OFFSET_2) & 0xff, fp) < 0) {
1458 IMAGE_LOGD("Error writing EXIF block length to file!");
1459 ReleaseExifDataBuffer(exifDataBuf);
1460 return false;
1461 }
1462
1463 // Write EXIF data block
1464 if (fwrite(exifDataBuf, exifDataBufLength, 1, fp) != 1) {
1465 IMAGE_LOGD("Error writing EXIF data block to file!");
1466 ReleaseExifDataBuffer(exifDataBuf);
1467 return false;
1468 }
1469 // Write JPEG image data, skipping the non-EXIF header
1470 unsigned int dataOffset = orginExifDataLength + sizeof(EXIF_HEADER);
1471 if (fwrite(buf + dataOffset, fileLength - dataOffset, 1, fp) != 1) {
1472 IMAGE_LOGD("Error writing JPEG image data to file!");
1473 ReleaseExifDataBuffer(exifDataBuf);
1474 return false;
1475 }
1476
1477 UpdateCacheExifData(fp);
1478 ReleaseExifDataBuffer(exifDataBuf);
1479 return true;
1480 }
1481
ReleaseSource(unsigned char ** ptrBuf,FILE ** ptrFile)1482 void EXIFInfo::ReleaseSource(unsigned char **ptrBuf, FILE **ptrFile)
1483 {
1484 if (*ptrBuf) {
1485 free(*ptrBuf);
1486 *ptrBuf = nullptr;
1487 ptrBuf = nullptr;
1488 }
1489
1490 if (*ptrFile != nullptr) {
1491 fclose(*ptrFile);
1492 *ptrFile = nullptr;
1493 ptrFile = nullptr;
1494 }
1495 }
1496
UpdateCacheExifData(FILE * fp)1497 void EXIFInfo::UpdateCacheExifData(FILE *fp)
1498 {
1499 unsigned long fileLength = GetFileSize(fp);
1500 if (fileLength == 0 || fileLength > MAX_FILE_SIZE) {
1501 IMAGE_LOGD("Get file size failed.");
1502 return;
1503 }
1504
1505 unsigned char *fileBuf = static_cast<unsigned char *>(malloc(fileLength));
1506 if (fileBuf == nullptr) {
1507 IMAGE_LOGD("Allocate buf failed.");
1508 return;
1509 }
1510
1511 // Set current position to begin of file.
1512 (void)fseek(fp, 0L, 0);
1513 if (fread(fileBuf, fileLength, 1, fp) != 1) {
1514 IMAGE_LOGD("Read new file failed.");
1515 free(fileBuf);
1516 fileBuf = nullptr;
1517 return;
1518 }
1519
1520 ParseExifData(fileBuf, static_cast<unsigned int>(fileLength));
1521 free(fileBuf);
1522 fileBuf = nullptr;
1523 }
1524
GetFilterArea(const uint8_t * buf,const uint32_t & bufSize,const int & privacyType,std::vector<std::pair<uint32_t,uint32_t>> & ranges)1525 uint32_t EXIFInfo::GetFilterArea(const uint8_t *buf,
1526 const uint32_t &bufSize,
1527 const int &privacyType,
1528 std::vector<std::pair<uint32_t, uint32_t>> &ranges)
1529 {
1530 std::unique_ptr<ByteOrderedBuffer> byteOrderedBuffer = std::make_unique<ByteOrderedBuffer>(buf, bufSize);
1531 byteOrderedBuffer->GenerateDEArray();
1532 if (byteOrderedBuffer->directoryEntryArray_.size() == 0) {
1533 IMAGE_LOGD("Read Exif info range failed.");
1534 return ERROR_PARSE_EXIF_FAILED;
1535 }
1536
1537 GetAreaFromExifEntries(privacyType, byteOrderedBuffer->directoryEntryArray_, ranges);
1538 if (ranges.size() == 0) {
1539 IMAGE_LOGD("There is no exif info need filtered in this image.");
1540 return ERROR_NO_EXIF_TAGS;
1541 }
1542
1543 return Media::SUCCESS;
1544 }
1545
GetAreaFromExifEntries(const int & privacyType,const std::vector<DirectoryEntry> & entryArray,std::vector<std::pair<uint32_t,uint32_t>> & ranges)1546 void EXIFInfo::GetAreaFromExifEntries(const int &privacyType,
1547 const std::vector<DirectoryEntry> &entryArray,
1548 std::vector<std::pair<uint32_t, uint32_t>> &ranges)
1549 {
1550 if (privacyType == PERMISSION_GPS_TYPE) {
1551 for (size_t i = 0; i < entryArray.size(); i++) {
1552 if (entryArray[i].ifd == EXIF_IFD_GPS) {
1553 std::pair<uint32_t, uint32_t> range =
1554 std::make_pair(entryArray[i].valueOffset, entryArray[i].valueLength);
1555 ranges.push_back(range);
1556 }
1557 }
1558 }
1559 }
1560
ByteOrderedBuffer(const uint8_t * fileBuf,uint32_t bufferLength)1561 ByteOrderedBuffer::ByteOrderedBuffer(const uint8_t *fileBuf, uint32_t bufferLength)
1562 : buf_(fileBuf), bufferLength_(bufferLength)
1563 {
1564 if (bufferLength >= BUFFER_POSITION_12 && bufferLength >= BUFFER_POSITION_13) {
1565 if (fileBuf[BUFFER_POSITION_12] == 'M' && fileBuf[BUFFER_POSITION_13] == 'M') {
1566 byteOrder_ = EXIF_BYTE_ORDER_MOTOROLA;
1567 } else {
1568 byteOrder_ = EXIF_BYTE_ORDER_INTEL;
1569 }
1570 }
1571 }
1572
~ByteOrderedBuffer()1573 ByteOrderedBuffer::~ByteOrderedBuffer() {}
1574
GenerateDEArray()1575 void ByteOrderedBuffer::GenerateDEArray()
1576 {
1577 // Move current position to begin of IFD0 offset segment
1578 curPosition_ = TIFF_OFFSET_FROM_FILE_BEGIN + CONSTANT_4;
1579 int32_t ifd0Offset = ReadInt32();
1580 if (ifd0Offset < 0) {
1581 IMAGE_LOGD("Get IFD0 offset failed!");
1582 return;
1583 }
1584 // Transform tiff offset to position of file
1585 ifd0Offset = static_cast<int32_t>(TransformTiffOffsetToFilePos(ifd0Offset));
1586 // Move current position to begin of IFD0 segment
1587 curPosition_ = static_cast<uint32_t>(ifd0Offset);
1588
1589 if (curPosition_ + CONSTANT_2 > bufferLength_) {
1590 IMAGE_LOGD("There is no data from the offset: %{public}d.", curPosition_);
1591 return;
1592 }
1593 GetDataRangeFromIFD(EXIF_IFD_0);
1594 }
1595
GetDataRangeFromIFD(const ExifIfd & ifd)1596 void ByteOrderedBuffer::GetDataRangeFromIFD(const ExifIfd &ifd)
1597 {
1598 handledIfdOffsets_.push_back(curPosition_);
1599 int16_t entryCount = ReadShort();
1600 if (static_cast<uint32_t>(curPosition_ + BYTE_COUNTS_12 * entryCount) > bufferLength_ || entryCount <= 0) {
1601 IMAGE_LOGD(" The size of entries is either too big or negative.");
1602 return;
1603 }
1604 GetDataRangeFromDE(ifd, entryCount);
1605
1606 if (Peek() + CONSTANT_4 <= bufferLength_) {
1607 int32_t nextIfdOffset = ReadInt32();
1608 if (nextIfdOffset == 0) {
1609 IMAGE_LOGD("Stop reading file since this IFD is finished");
1610 return;
1611 }
1612 // Transform tiff offset to position of file
1613 if (nextIfdOffset != -1) {
1614 nextIfdOffset = static_cast<int32_t>(TransformTiffOffsetToFilePos(nextIfdOffset));
1615 }
1616 // Check if the next IFD offset
1617 // 1. Exists within the boundaries of the buffer
1618 // 2. Does not point to a previously read IFD.
1619 if (nextIfdOffset > 0L && static_cast<uint32_t>(nextIfdOffset) < bufferLength_) {
1620 if (!IsIFDhandled(nextIfdOffset)) {
1621 curPosition_ = static_cast<uint32_t>(nextIfdOffset);
1622 ExifIfd nextIfd = GetNextIfdFromLinkList(ifd);
1623 GetDataRangeFromIFD(nextIfd);
1624 } else {
1625 IMAGE_LOGD("Stop reading buffer since re-reading an IFD at %{public}d.", nextIfdOffset);
1626 }
1627 } else {
1628 IMAGE_LOGD("Stop reading file since a wrong offset at %{public}d.", nextIfdOffset);
1629 }
1630 }
1631 }
1632
GetDataRangeFromDE(const ExifIfd & ifd,const int16_t & count)1633 void ByteOrderedBuffer::GetDataRangeFromDE(const ExifIfd &ifd, const int16_t &count)
1634 {
1635 for (int16_t i = 0; i < count; i++) {
1636 uint16_t tagNumber = ReadUnsignedShort();
1637 uint16_t dataFormat = ReadUnsignedShort();
1638 int32_t numberOfComponents = ReadInt32();
1639 uint32_t nextEntryOffset = Peek() + CONSTANT_4;
1640
1641 uint32_t byteCount = 0;
1642 bool valid = SetDEDataByteCount(tagNumber, dataFormat, numberOfComponents, byteCount);
1643 if (!valid) {
1644 curPosition_ = static_cast<uint32_t>(nextEntryOffset);
1645 continue;
1646 }
1647
1648 // Read a value from data field or seek to the value offset which is stored in data
1649 // field if the size of the entry value is bigger than 4.
1650 // If the size of the entry value is less than 4, value is stored in value offset area.
1651 if (byteCount > CONSTANT_4) {
1652 int32_t offset = ReadInt32();
1653 // Transform tiff offset to position of file
1654 if (offset != -1) {
1655 offset = static_cast<int32_t>(TransformTiffOffsetToFilePos(offset));
1656 }
1657 if ((static_cast<uint32_t>(offset) + byteCount) <= bufferLength_) {
1658 curPosition_ = static_cast<uint32_t>(offset);
1659 } else {
1660 // Skip if invalid data offset.
1661 IMAGE_LOGI("Skip the tag entry since data offset is invalid: %{public}d.", offset);
1662 curPosition_ = nextEntryOffset;
1663 continue;
1664 }
1665 }
1666
1667 // Recursively parse IFD when a IFD pointer tag appears
1668 if (IsIFDPointerTag(tagNumber)) {
1669 ExifIfd ifdOfIFDPointerTag = GetIFDOfIFDPointerTag(tagNumber);
1670 ParseIFDPointerTag(ifdOfIFDPointerTag, dataFormat);
1671 curPosition_ = nextEntryOffset;
1672 continue;
1673 }
1674
1675 uint32_t bytesOffset = Peek();
1676 uint32_t bytesLength = byteCount;
1677 DirectoryEntry directoryEntry = {static_cast<ExifTag>(tagNumber), static_cast<ExifFormat>(dataFormat),
1678 numberOfComponents, bytesOffset, bytesLength, ifd};
1679 directoryEntryArray_.push_back(directoryEntry);
1680
1681 // Seek to next tag offset
1682 if (Peek() != nextEntryOffset) {
1683 curPosition_ = nextEntryOffset;
1684 }
1685 }
1686 }
1687
TransformTiffOffsetToFilePos(const uint32_t & offset)1688 uint32_t ByteOrderedBuffer::TransformTiffOffsetToFilePos(const uint32_t &offset)
1689 {
1690 return offset + TIFF_OFFSET_FROM_FILE_BEGIN;
1691 }
1692
SetDEDataByteCount(const uint16_t & tagNumber,const uint16_t & dataFormat,const int32_t & numberOfComponents,uint32_t & count)1693 bool ByteOrderedBuffer::SetDEDataByteCount(const uint16_t &tagNumber,
1694 const uint16_t &dataFormat,
1695 const int32_t &numberOfComponents,
1696 uint32_t &count)
1697 {
1698 if (IsValidTagNumber(tagNumber)) {
1699 IMAGE_LOGD("Skip the tag entry since tag number is not defined: %{public}d.", tagNumber);
1700 } else if (dataFormat <= 0 || exif_format_get_size(static_cast<ExifFormat>(dataFormat)) == 0) {
1701 IMAGE_LOGD("Skip the tag entry since data format is invalid: %{public}d.", dataFormat);
1702 } else {
1703 count = static_cast<uint32_t>(numberOfComponents) *
1704 static_cast<uint32_t>(exif_format_get_size(static_cast<ExifFormat>(dataFormat)));
1705 if (count < 0) {
1706 IMAGE_LOGD("Skip the tag entry since the number of components is invalid: %{public}d.",
1707 numberOfComponents);
1708 } else {
1709 return true;
1710 }
1711 }
1712 return false;
1713 }
1714
ParseIFDPointerTag(const ExifIfd & ifd,const uint16_t & dataFormat)1715 void ByteOrderedBuffer::ParseIFDPointerTag(const ExifIfd &ifd, const uint16_t &dataFormat)
1716 {
1717 uint32_t offset = 0U;
1718 // Get offset from data field
1719 switch (static_cast<ExifFormat>(dataFormat)) {
1720 case EXIF_FORMAT_SHORT: {
1721 offset = static_cast<uint32_t>(ReadUnsignedShort());
1722 break;
1723 }
1724 case EXIF_FORMAT_SSHORT: {
1725 offset = ReadShort();
1726 break;
1727 }
1728 case EXIF_FORMAT_LONG: {
1729 offset = ReadUnsignedInt32();
1730 break;
1731 }
1732 case EXIF_FORMAT_SLONG: {
1733 offset = static_cast<uint32_t>(ReadInt32());
1734 break;
1735 }
1736 default: {
1737 // Nothing to do
1738 break;
1739 }
1740 }
1741 // Transform tiff offset to position of file
1742 offset = TransformTiffOffsetToFilePos(offset);
1743 // Check if the next IFD offset
1744 // 1. Exists within the boundaries of the buffer
1745 // 2. Does not point to a previously read IFD.
1746 if (offset > 0L && offset < bufferLength_) {
1747 if (!IsIFDhandled(offset)) {
1748 curPosition_ = offset;
1749 GetDataRangeFromIFD(ifd);
1750 } else {
1751 IMAGE_LOGD("Skip jump into the IFD since it has already been read at %{public}d.", offset);
1752 }
1753 } else {
1754 IMAGE_LOGD("Skip jump into the IFD since its offset is invalid: %{public}d.", offset);
1755 }
1756 }
1757
IsValidTagNumber(const uint16_t & tagNumber)1758 bool ByteOrderedBuffer::IsValidTagNumber(const uint16_t &tagNumber)
1759 {
1760 for (uint32_t i = 0; (IsSameTextStr(EXIF_TAG_TABLE[i].name, "")); i++) {
1761 if (EXIF_TAG_TABLE[i].number == tagNumber) {
1762 return true;
1763 }
1764 }
1765 return false;
1766 }
1767
IsIFDhandled(const uint32_t & position)1768 bool ByteOrderedBuffer::IsIFDhandled(const uint32_t &position)
1769 {
1770 if (handledIfdOffsets_.size() == 0) {
1771 IMAGE_LOGD("There is no handled IFD!");
1772 return false;
1773 }
1774
1775 for (size_t i = 0; i < handledIfdOffsets_.size(); i++) {
1776 if (handledIfdOffsets_[i] == position) {
1777 return true;
1778 }
1779 }
1780 return false;
1781 }
1782
IsIFDPointerTag(const uint16_t & tagNumber)1783 bool ByteOrderedBuffer::IsIFDPointerTag(const uint16_t &tagNumber)
1784 {
1785 bool ret = false;
1786 switch (static_cast<ExifTag>(tagNumber)) {
1787 case EXIF_TAG_SUB_IFDS:
1788 case EXIF_TAG_EXIF_IFD_POINTER:
1789 case EXIF_TAG_GPS_INFO_IFD_POINTER:
1790 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
1791 ret = true;
1792 break;
1793 default:
1794 break;
1795 }
1796 return ret;
1797 }
1798
GetIFDOfIFDPointerTag(const uint16_t & tagNumber)1799 ExifIfd ByteOrderedBuffer::GetIFDOfIFDPointerTag(const uint16_t &tagNumber)
1800 {
1801 ExifIfd ifd = EXIF_IFD_COUNT;
1802 switch (static_cast<ExifTag>(tagNumber)) {
1803 case EXIF_TAG_EXIF_IFD_POINTER: {
1804 ifd = EXIF_IFD_EXIF;
1805 break;
1806 }
1807 case EXIF_TAG_GPS_INFO_IFD_POINTER: {
1808 ifd = EXIF_IFD_GPS;
1809 break;
1810 }
1811 case EXIF_TAG_INTEROPERABILITY_IFD_POINTER: {
1812 ifd = EXIF_IFD_INTEROPERABILITY;
1813 break;
1814 }
1815 default: {
1816 break;
1817 }
1818 }
1819 return ifd;
1820 }
1821
GetNextIfdFromLinkList(const ExifIfd & ifd)1822 ExifIfd ByteOrderedBuffer::GetNextIfdFromLinkList(const ExifIfd &ifd)
1823 {
1824 ExifIfd nextIfd = EXIF_IFD_COUNT;
1825 /**
1826 * In jpeg file, the next IFD of IFD_0 in IFD link list is IFD_1,
1827 * other IFD is pointed by tag.
1828 */
1829 if (ifd == EXIF_IFD_0) {
1830 nextIfd = EXIF_IFD_1;
1831 }
1832 return nextIfd;
1833 }
1834
ReadInt32()1835 int32_t ByteOrderedBuffer::ReadInt32()
1836 {
1837 // Move current position to begin of next segment
1838 curPosition_ += CONSTANT_4;
1839 if (curPosition_ > bufferLength_) {
1840 IMAGE_LOGD("Current Position %{public}u out of range.", curPosition_);
1841 return -1;
1842 }
1843
1844 if (byteOrder_ == EXIF_BYTE_ORDER_MOTOROLA) {
1845 return ((static_cast<unsigned int>(buf_[curPosition_ - CONSTANT_4]) << MOVE_OFFSET_24) |
1846 (static_cast<unsigned int>(buf_[curPosition_ - CONSTANT_3]) << MOVE_OFFSET_16) |
1847 (static_cast<unsigned int>(buf_[curPosition_ - CONSTANT_2]) << MOVE_OFFSET_8) |
1848 static_cast<unsigned int>(buf_[curPosition_ - 1]));
1849 } else {
1850 return ((static_cast<unsigned int>(buf_[curPosition_ - 1]) << MOVE_OFFSET_24) |
1851 (static_cast<unsigned int>(buf_[curPosition_ - CONSTANT_2]) << MOVE_OFFSET_16) |
1852 (static_cast<unsigned int>(buf_[curPosition_ - CONSTANT_3]) << MOVE_OFFSET_8) |
1853 static_cast<unsigned int>(buf_[curPosition_ - CONSTANT_4]));
1854 }
1855 }
1856
ReadUnsignedInt32()1857 uint32_t ByteOrderedBuffer::ReadUnsignedInt32()
1858 {
1859 return (static_cast<uint32_t>(ReadInt32()) & 0xffffffff);
1860 }
1861
ReadShort()1862 int16_t ByteOrderedBuffer::ReadShort()
1863 {
1864 // Move current position to begin of next segment
1865 curPosition_ += CONSTANT_2;
1866 if (curPosition_ > bufferLength_) {
1867 IMAGE_LOGD("Current Position %{public}u out of range.", curPosition_);
1868 return -1;
1869 }
1870
1871 if (byteOrder_ == EXIF_BYTE_ORDER_MOTOROLA) {
1872 return ((static_cast<unsigned int>(buf_[curPosition_ - CONSTANT_2]) << MOVE_OFFSET_8) |
1873 static_cast<unsigned int>(buf_[curPosition_ - 1]));
1874 } else {
1875 return ((static_cast<unsigned int>(buf_[curPosition_ - 1]) << MOVE_OFFSET_8) |
1876 static_cast<unsigned int>(buf_[curPosition_ - CONSTANT_2]));
1877 }
1878 }
1879
ReadUnsignedShort()1880 uint16_t ByteOrderedBuffer::ReadUnsignedShort()
1881 {
1882 return (static_cast<uint32_t>(ReadShort()) & 0xffff);
1883 }
1884
Peek()1885 uint32_t ByteOrderedBuffer::Peek()
1886 {
1887 return curPosition_;
1888 }
1889
CheckExifEntryValid(const ExifIfd & ifd,const ExifTag & tag)1890 bool EXIFInfo::CheckExifEntryValid(const ExifIfd &ifd, const ExifTag &tag)
1891 {
1892 bool ret = false;
1893 switch (ifd) {
1894 case EXIF_IFD_0: {
1895 if (tag == EXIF_TAG_ORIENTATION ||
1896 tag == EXIF_TAG_BITS_PER_SAMPLE ||
1897 tag == EXIF_TAG_IMAGE_LENGTH ||
1898 tag == EXIF_TAG_IMAGE_WIDTH) {
1899 ret = true;
1900 }
1901 break;
1902 }
1903 case EXIF_IFD_EXIF: {
1904 if (tag == EXIF_TAG_DATE_TIME_ORIGINAL ||
1905 tag == EXIF_TAG_EXPOSURE_TIME ||
1906 tag == EXIF_TAG_FNUMBER ||
1907 tag == EXIF_TAG_ISO_SPEED_RATINGS ||
1908 tag == EXIF_TAG_SCENE_TYPE ||
1909 tag == EXIF_TAG_COMPRESSED_BITS_PER_PIXEL) {
1910 ret = true;
1911 }
1912 break;
1913 }
1914 case EXIF_IFD_GPS: {
1915 if (tag == EXIF_TAG_GPS_LATITUDE ||
1916 tag == EXIF_TAG_GPS_LONGITUDE ||
1917 tag == EXIF_TAG_GPS_LATITUDE_REF ||
1918 tag == EXIF_TAG_GPS_LONGITUDE_REF) {
1919 ret = true;
1920 }
1921 break;
1922 }
1923 default:
1924 break;
1925 }
1926
1927 if (!ret) {
1928 ret = CheckExifEntryValidEx(ifd, tag);
1929 }
1930
1931 return ret;
1932 }
1933
CheckExifEntryValidEx(const ExifIfd & ifd,const ExifTag & tag)1934 bool EXIFInfo::CheckExifEntryValidEx(const ExifIfd &ifd, const ExifTag &tag)
1935 {
1936 static std::vector<ExifTag> ifd0TagVec{EXIF_TAG_DATE_TIME, EXIF_TAG_IMAGE_DESCRIPTION, EXIF_TAG_MAKE,
1937 EXIF_TAG_MODEL};
1938 static std::vector<ExifTag> ifdExifTagVec{TAG_SENSITIVITY_TYPE, TAG_STANDARD_OUTPUT_SENSITIVITY,
1939 TAG_RECOMMENDED_EXPOSURE_INDEX, EXIF_TAG_APERTURE_VALUE, EXIF_TAG_EXPOSURE_BIAS_VALUE, EXIF_TAG_METERING_MODE,
1940 EXIF_TAG_LIGHT_SOURCE, EXIF_TAG_FLASH, EXIF_TAG_FOCAL_LENGTH, EXIF_TAG_USER_COMMENT, EXIF_TAG_PIXEL_X_DIMENSION,
1941 EXIF_TAG_PIXEL_Y_DIMENSION, EXIF_TAG_WHITE_BALANCE, EXIF_TAG_FOCAL_LENGTH_IN_35MM_FILM};
1942 bool ret = false;
1943 switch (ifd) {
1944 case EXIF_IFD_0: {
1945 return std::find(ifd0TagVec.begin(), ifd0TagVec.end(), tag) != ifd0TagVec.end();
1946 }
1947 case EXIF_IFD_EXIF: {
1948 return std::find(ifdExifTagVec.begin(), ifdExifTagVec.end(), tag) != ifdExifTagVec.end();
1949 }
1950 case EXIF_IFD_GPS: {
1951 return tag == EXIF_TAG_GPS_TIME_STAMP || tag == EXIF_TAG_GPS_DATE_STAMP;
1952 }
1953 default:
1954 break;
1955 }
1956
1957 return ret;
1958 }
1959
NumSplit(std::string & src,std::vector<std::string> & out)1960 static void NumSplit(std::string &src, std::vector<std::string> &out)
1961 {
1962 if (src.size() == 0) {
1963 return;
1964 }
1965 std::vector<std::string> res;
1966 size_t last = 0;
1967 for (size_t i = 0; i < src.size(); i++) {
1968 if (!std::isdigit(src[i])) {
1969 size_t splitSize = i - last;
1970 if (splitSize != 0) {
1971 res.push_back(src.substr(last, splitSize));
1972 }
1973 last = i + SIZE_1;
1974 }
1975 }
1976 if (last <= (src.size() - SIZE_1)) {
1977 res.push_back(src.substr(last));
1978 }
1979 for (size_t i = 0; i < res.size() && i < out.size(); i++) {
1980 out[i] = res[i];
1981 }
1982 }
1983
JoinStr(std::vector<std::string> & in,const std::string & delim)1984 static std::string JoinStr(std::vector<std::string> &in, const std::string &delim)
1985 {
1986 std::string res = "";
1987 for (size_t i = 0; i < (in.size() - SIZE_1); i++) {
1988 res.append(in[i]).append(delim);
1989 }
1990 res.append(in.back());
1991 return res;
1992 }
1993
FormatTimeStamp(std::string & src,std::string & value)1994 static void FormatTimeStamp(std::string &src, std::string &value)
1995 {
1996 std::string date = src;
1997 std::string time = "";
1998 std::string::size_type position = src.find(" ");
1999 if (position != src.npos) {
2000 // Date and time
2001 date = src.substr(0, position);
2002 time = src.substr(position);
2003 }
2004 std::vector<std::string> dateVector = {"1970", "01", "01"};
2005 std::vector<std::string> timeVector = {"00", "00", "00"};
2006 NumSplit(date, dateVector);
2007 NumSplit(time, timeVector);
2008 value = JoinStr(dateVector, "-") + " " + JoinStr(timeVector, ":");
2009 }
2010
SpecialExifData(EXIFInfo * info,const std::string & name,std::string & value)2011 static uint32_t SpecialExifData(EXIFInfo* info, const std::string &name, std::string &value)
2012 {
2013 if (IsSameTextStr(DATE_TIME_ORIGINAL_MEDIA, name)) {
2014 std::string orgValue;
2015 auto res = info->GetExifData(DATE_TIME_ORIGINAL, orgValue);
2016 if (res == Media::SUCCESS) {
2017 FormatTimeStamp(orgValue, value);
2018 }
2019 return res;
2020 } else if (IsSameTextStr(TAG_ORIENTATION_INT, name)) {
2021 std::string orgValue;
2022 auto res = info->GetExifData(TAG_ORIENTATION_STRING, orgValue);
2023 if (res != Media::SUCCESS) {
2024 return res;
2025 }
2026 if (ORIENTATION_INT_MAP.count(orgValue) == 0) {
2027 IMAGE_LOGD("SpecialExifData %{public}s not found %{public}s.",
2028 name.c_str(), orgValue.c_str());
2029 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
2030 }
2031 value = std::to_string(ORIENTATION_INT_MAP.at(orgValue));
2032 return res;
2033 }
2034 return Media::ERR_MEDIA_STATUS_ABNORMAL;
2035 }
2036
GetExifTagByName(const std::string & name,ExifTag & tag)2037 static bool GetExifTagByName(const std::string &name, ExifTag &tag)
2038 {
2039 auto find_item = std::find_if(TAG_MAP.begin(), TAG_MAP.end(),
2040 [name](const std::map<ExifTag, std::string>::value_type item) {
2041 return IsSameTextStr(item.second, name);
2042 });
2043 auto find_maker_item = std::find_if(TAG_MAKER_NOTE_MAP.begin(), TAG_MAKER_NOTE_MAP.end(),
2044 [name](const std::map<ExifTag, std::string>::value_type item) {
2045 return IsSameTextStr(item.second, name);
2046 });
2047 if (find_item == TAG_MAP.end() && find_maker_item == TAG_MAKER_NOTE_MAP.end()) {
2048 return false;
2049 }
2050 tag = find_item->first;
2051 return true;
2052 }
2053
GetExifData(const std::string name,std::string & value)2054 uint32_t EXIFInfo::GetExifData(const std::string name, std::string &value)
2055 {
2056 auto res = SpecialExifData(this, name, value);
2057 if (res == Media::SUCCESS || res != Media::ERR_MEDIA_STATUS_ABNORMAL) {
2058 IMAGE_LOGD("GetExifData %{public}s special result with %{public}d.", name.c_str(), res);
2059 return res;
2060 }
2061 ExifTag tag;
2062 if (!GetExifTagByName(name, tag)) {
2063 IMAGE_LOGD("GetExifData %{public}s not in the TAGs map.", name.c_str());
2064 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
2065 }
2066 DumpTagsMap(exifTags_);
2067 if (exifTags_.count(tag) == 0) {
2068 IMAGE_LOGD("GetExifData has no tag %{public}s[%{public}d], tags Size: %{public}zu.",
2069 name.c_str(), tag, exifTags_.size());
2070 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
2071 }
2072 value = exifTags_.at(tag);
2073 if (IsSameTextStr(value, DEFAULT_EXIF_VALUE)) {
2074 IMAGE_LOGD("GetExifData %{public}s[%{public}d] value is DEFAULT_EXIF_VALUE.",
2075 name.c_str(), tag);
2076 return Media::ERR_MEDIA_VALUE_INVALID;
2077 }
2078 return Media::SUCCESS;
2079 }
2080
ModifyExifData(const std::string name,const std::string & value,const std::string & path)2081 uint32_t EXIFInfo::ModifyExifData(const std::string name, const std::string &value, const std::string &path)
2082 {
2083 ExifTag tag;
2084 if (!GetExifTagByName(name, tag)) {
2085 IMAGE_LOGD("ModifyExifData %{public}s not in the TAGs map.", name.c_str());
2086 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
2087 }
2088 return ModifyExifData(tag, value, path);
2089 }
2090
ModifyExifData(const std::string name,const std::string & value,const int fd)2091 uint32_t EXIFInfo::ModifyExifData(const std::string name, const std::string &value, const int fd)
2092 {
2093 ExifTag tag;
2094 if (!GetExifTagByName(name, tag)) {
2095 IMAGE_LOGD("ModifyExifData %{public}s not in the TAGs map.", name.c_str());
2096 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
2097 }
2098 return ModifyExifData(tag, value, fd);
2099 }
2100
ModifyExifData(const std::string name,const std::string & value,unsigned char * data,uint32_t size)2101 uint32_t EXIFInfo::ModifyExifData(const std::string name, const std::string &value, unsigned char *data, uint32_t size)
2102 {
2103 ExifTag tag;
2104 if (!GetExifTagByName(name, tag)) {
2105 IMAGE_LOGD("ModifyExifData %{public}s not in the TAGs map.", name.c_str());
2106 return Media::ERR_IMAGE_DECODE_EXIF_UNSUPPORT;
2107 }
2108 return ModifyExifData(tag, value, data, size);
2109 }
2110 } // namespace ImagePlugin
2111 } // namespace OHOS
2112