1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FRAMEWORKS_INNERKITSIMPL_ACCESSOR_INCLUDE_PNG_IMAGE_CHUNK_UTILS_H
17 #define FRAMEWORKS_INNERKITSIMPL_ACCESSOR_INCLUDE_PNG_IMAGE_CHUNK_UTILS_H
18 
19 #include <algorithm>
20 #include <cstddef>
21 #include <cstdint>
22 #include <fstream>
23 #include <map>
24 #include <stdint.h>
25 #include <vector>
26 
27 #include <libexif/exif-data.h>
28 
29 #include "data_buf.h"
30 
31 namespace OHOS {
32 namespace Media {
33 class PngImageChunkUtils {
34 public:
35     // type of png text chunk
36     enum TextChunkType { tEXtChunk = 0, zTXtChunk = 1, iTXtChunk = 2 };
37 
38     // parse exif meta data from text type chunk (tEXt/zTXt/iTXt)
39     static int ParseTextChunk(const DataBuf &chunkData, TextChunkType chunkType,
40         DataBuf &tiffData, bool &isCompressed);
41 
42     // lookup text chunk's exif keyword "raw profile xxx"
43     static bool FindExifFromTxt(DataBuf &chunkData);
44 private:
45     // get keyword from png txt chunk
46     static DataBuf GetKeywordFromChunk(const DataBuf &chunkData);
47 
48     // get data from zTxt chunk
49     static DataBuf GetRawTextFromZtxtChunk(const DataBuf &chunkData, size_t keySize,
50         DataBuf &rawText, bool &isCompressed);
51 
52     // get data from tExt chunk
53     static DataBuf GetRawTextFromTextChunk(const DataBuf &chunkData, size_t keySize, DataBuf &rawText);
54 
55     // get data from iTxt chunk
56     static DataBuf GetRawTextFromItxtChunk(const DataBuf &chunkData, size_t keySize,
57         DataBuf &rawText, bool &isCompressed);
58 
59     // get text field from png txt chunk
60     static DataBuf GetRawTextFromChunk(const DataBuf &chunkData, size_t keySize,
61         TextChunkType chunkType, bool &isCompressed);
62 
63     // lookup exif keyword
64     static bool FindExifKeyword(const byte *keyword, size_t size);
65 
66     // lookup exif marker
67     static size_t VerifyExifIdCode(DataBuf &exifInfo, size_t exifInfoLength);
68 
69     // parse Exif Tiff metadata from data in png txt chunk
70     static int GetTiffDataFromRawText(const DataBuf &rawText, DataBuf &tiffData);
71 
72     // decompress with zlib
73     static int DecompressText(const byte *sourceData, unsigned int sourceDataLen, DataBuf &textOut);
74 
75     // step over newline character
76     static const char *StepOverNewLine(const char *sourcePtr, const char *endPtr);
77 
78     // get exif length
79     static const char *GetExifInfoLen(const char *sourcePtr, size_t *lengthOut, const char *endPtr);
80 
81     // convert sting to digit
82     static int ConvertAsciiToInt(const char *sourcePtr, size_t exifInfoLength, unsigned char *destPtr);
83 
84     // convert Exif metadata from Ascii char to hex
85     static DataBuf ConvertRawTextToExifInfo(const DataBuf &rawText);
86 };
87 } // namespace Media
88 } // namespace OHOS
89 
90 #endif // FRAMEWORKS_INNERKITSIMPL_ACCESSOR_INCLUDE_PNG_IMAGE_CHUNK_UTILS_H
91