Skip to content

Commit 25c47cd

Browse files
authored
Merge pull request #2197 from Exiv2/mainRefactoringFormats
Refactoring in JpegImage and Photoshop classes
2 parents 84ba579 + f07c88d commit 25c47cd

File tree

17 files changed

+339
-322
lines changed

17 files changed

+339
-322
lines changed

app/exiv2.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "xmp_exiv2.hpp"
1414

1515
#include <algorithm>
16+
#include <array>
1617
#include <cctype>
1718
#include <cstring>
1819
#include <fstream>

include/exiv2/exiv2.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "exiv2/mrwimage.hpp"
3030
#include "exiv2/orfimage.hpp"
3131
#include "exiv2/pgfimage.hpp"
32+
#include "exiv2/photoshop.hpp"
3233

3334
#ifdef EXV_HAVE_LIBZ
3435
#include "exiv2/pngimage.hpp"

include/exiv2/jpgimage.hpp

Lines changed: 3 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
#ifndef JPGIMAGE_HPP_
44
#define JPGIMAGE_HPP_
55

6-
// *****************************************************************************
76
#include "exiv2lib_export.h"
87

9-
#include <array>
10-
118
// included header files
129
#include "error.hpp"
1310
#include "image.hpp"
@@ -18,59 +15,6 @@ namespace Exiv2 {
1815
// *****************************************************************************
1916
// class definitions
2017

21-
/// @brief Helper class, has methods to deal with %Photoshop "Information Resource Blocks" (IRBs).
22-
struct EXIV2API Photoshop {
23-
// Todo: Public for now
24-
static constexpr std::array irbId_{"8BIM", "AgHg", "DCSR", "PHUT"}; //!< %Photoshop IRB markers
25-
static constexpr auto ps3Id_ = "Photoshop 3.0\0"; //!< %Photoshop marker
26-
static constexpr uint16_t iptc_ = 0x0404; //!< %Photoshop IPTC marker
27-
static constexpr uint16_t preview_ = 0x040c; //!< %Photoshop preview marker
28-
29-
/// @brief Checks an IRB
30-
/// @param pPsData Existing IRB buffer. It is expected to be of size 4.
31-
/// @return true if the IRB marker is known
32-
/// @todo This should be an implementation detail and not exposed in the API. An attacker could try to pass
33-
/// a smaller buffer or null pointer.
34-
static bool isIrb(const byte* pPsData);
35-
36-
/// @brief Validates all IRBs
37-
/// @param pPsData Existing IRB buffer
38-
/// @param sizePsData Size of the IRB buffer, may be 0
39-
/// @return true if all IRBs are valid;<BR> false otherwise
40-
static bool valid(const byte* pPsData, size_t sizePsData);
41-
42-
/// @brief Locates the data for a %Photoshop tag in a %Photoshop formated memory buffer.
43-
/// Operates on raw data to simplify reuse.
44-
/// @param pPsData Pointer to buffer containing entire payload of %Photoshop formated data (from APP13 Jpeg segment)
45-
/// @param sizePsData Size in bytes of pPsData.
46-
/// @param psTag %Tag number of the block to look for.
47-
/// @param record Output value that is set to the start of the data block within pPsData (may not be null).
48-
/// @param sizeHdr Output value that is set to the size of the header within the data block pointed to by record
49-
/// (may not be null).
50-
/// @param sizeData Output value that is set to the size of the actual data within the data block pointed to by record
51-
/// (may not be null).
52-
/// @return 0 if successful;<BR>
53-
/// 3 if no data for psTag was found in pPsData;<BR>
54-
/// -2 if the pPsData buffer does not contain valid data.
55-
static int locateIrb(const byte* pPsData, size_t sizePsData, uint16_t psTag, const byte** record,
56-
uint32_t* const sizeHdr, uint32_t* const sizeData);
57-
58-
/// @brief Forwards to locateIrb() with \em psTag = \em iptc_
59-
static int locateIptcIrb(const byte* pPsData, size_t sizePsData, const byte** record, uint32_t* const sizeHdr,
60-
uint32_t* const sizeData);
61-
62-
/// @brief Forwards to locatePreviewIrb() with \em psTag = \em preview_
63-
static int locatePreviewIrb(const byte* pPsData, size_t sizePsData, const byte** record, uint32_t* const sizeHdr,
64-
uint32_t* const sizeData);
65-
66-
/// @brief Set the new IPTC IRB, keeps existing IRBs but removes the IPTC block if there is no new IPTC data to write.
67-
/// @param pPsData Existing IRB buffer
68-
/// @param sizePsData Size of the IRB buffer, may be 0
69-
/// @param iptcData Iptc data to embed, may be empty
70-
/// @return A data buffer containing the new IRB buffer, may have 0 size
71-
static DataBuf setIptcIrb(const byte* pPsData, size_t sizePsData, const IptcData& iptcData);
72-
};
73-
7418
/*!
7519
@brief Abstract helper base class to access JPEG images.
7620
*/
@@ -151,44 +95,6 @@ class EXIV2API JpegBase : public Image {
15195
virtual int writeHeader(BasicIo& oIo) const = 0;
15296
//@}
15397

154-
// Constant Data
155-
static constexpr byte dht_ = 0xc4; //!< JPEG DHT marker
156-
static constexpr byte dqt_ = 0xdb; //!< JPEG DQT marker
157-
static constexpr byte dri_ = 0xdd; //!< JPEG DRI marker
158-
static constexpr byte sos_ = 0xda; //!< JPEG SOS marker
159-
static constexpr byte eoi_ = 0xd9; //!< JPEG EOI marker
160-
static constexpr byte app0_ = 0xe0; //!< JPEG APP0 marker
161-
static constexpr byte app1_ = 0xe1; //!< JPEG APP1 marker
162-
static constexpr byte app2_ = 0xe2; //!< JPEG APP2 marker
163-
static constexpr byte app13_ = 0xed; //!< JPEG APP13 marker
164-
static constexpr byte com_ = 0xfe; //!< JPEG Comment marker
165-
166-
// Start of Frame markers, nondifferential Huffman-coding frames
167-
static constexpr byte sof0_ = 0xc0; //!< JPEG Start-Of-Frame marker
168-
static constexpr byte sof1_ = 0xc1; //!< JPEG Start-Of-Frame marker
169-
static constexpr byte sof2_ = 0xc2; //!< JPEG Start-Of-Frame marker
170-
static constexpr byte sof3_ = 0xc3; //!< JPEG Start-Of-Frame marker
171-
172-
// Start of Frame markers, differential Huffman-coding frames
173-
static constexpr byte sof5_ = 0xc5; //!< JPEG Start-Of-Frame marker
174-
static constexpr byte sof6_ = 0xc6; //!< JPEG Start-Of-Frame marker
175-
static constexpr byte sof7_ = 0xc7; //!< JPEG Start-Of-Frame marker
176-
177-
// Start of Frame markers, nondifferential arithmetic-coding frames
178-
static constexpr byte sof9_ = 0xc9; //!< JPEG Start-Of-Frame marker
179-
static constexpr byte sof10_ = 0xca; //!< JPEG Start-Of-Frame marker
180-
static constexpr byte sof11_ = 0xcb; //!< JPEG Start-Of-Frame marker
181-
182-
// Start of Frame markers, differential arithmetic-coding frames
183-
static constexpr byte sof13_ = 0xcd; //!< JPEG Start-Of-Frame marker
184-
static constexpr byte sof14_ = 0xce; //!< JPEG Start-Of-Frame marker
185-
static constexpr byte sof15_ = 0xcf; //!< JPEG Start-Of-Frame marker
186-
187-
static constexpr auto exifId_ = "Exif\0\0"; //!< Exif identifier
188-
static constexpr auto jfifId_ = "JFIF\0"; //!< JFIF identifier
189-
static constexpr auto xmpId_ = "http://ns.adobe.com/xap/1.0/\0"; //!< XMP packet identifier
190-
static constexpr auto iccId_ = "ICC_PROFILE\0"; //!< ICC profile identifier
191-
19298
private:
19399
//! @name Manipulators
194100
//@{
@@ -225,14 +131,7 @@ class EXIV2API JpegBase : public Image {
225131
//@}
226132

227133
DataBuf readNextSegment(byte marker);
228-
229-
/*!
230-
@brief Is the marker followed by a non-zero payload?
231-
@param marker The marker at the start of a segment
232-
@return true if the marker is followed by a non-zero payload
233-
*/
234-
static bool markerHasLength(byte marker);
235-
}; // class JpegBase
134+
};
236135

237136
/*!
238137
@brief Class to access JPEG images
@@ -292,9 +191,8 @@ class EXIV2API JpegImage : public JpegBase {
292191

293192
private:
294193
// Constant data
295-
static constexpr byte soi_ = 0xd8; // SOI marker
296-
static const byte blank_[]; // Minimal Jpeg image
297-
}; // class JpegImage
194+
static const byte blank_[]; ///< Minimal Jpeg image
195+
};
298196

299197
//! Helper class to access %Exiv2 files
300198
class EXIV2API ExvImage : public JpegBase {

include/exiv2/photoshop.hpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#ifndef PHOTOSHOP_INT_HPP
4+
#define PHOTOSHOP_INT_HPP
5+
6+
#include "exiv2lib_export.h"
7+
8+
#include "types.hpp"
9+
10+
#include <array>
11+
12+
namespace Exiv2 {
13+
// Forward declarations
14+
class IptcData;
15+
16+
/// @brief Helper class, has methods to deal with %Photoshop "Information Resource Blocks" (IRBs).
17+
struct EXIV2API Photoshop {
18+
// Todo: Public for now
19+
static constexpr std::array irbId_{"8BIM", "AgHg", "DCSR", "PHUT"}; //!< %Photoshop IRB markers
20+
static constexpr auto ps3Id_ = "Photoshop 3.0\0"; //!< %Photoshop marker
21+
static constexpr uint16_t iptc_ = 0x0404; //!< %Photoshop IPTC marker
22+
static constexpr uint16_t preview_ = 0x040c; //!< %Photoshop preview marker
23+
24+
/// @brief Checks an IRB
25+
/// @param pPsData Existing IRB buffer. It is expected to be of size 4.
26+
/// @return true if the IRB marker is known
27+
/// @todo This should be an implementation detail and not exposed in the API. An attacker could try to pass
28+
/// a smaller buffer or null pointer.
29+
static bool isIrb(const byte* pPsData);
30+
31+
/// @brief Validates all IRBs
32+
/// @param pPsData Existing IRB buffer
33+
/// @param sizePsData Size of the IRB buffer, may be 0
34+
/// @return true if all IRBs are valid;<BR> false otherwise
35+
static bool valid(const byte* pPsData, size_t sizePsData);
36+
37+
/// @brief Locates the data for a %Photoshop tag in a %Photoshop formated memory buffer.
38+
/// Operates on raw data to simplify reuse.
39+
/// @param pPsData Pointer to buffer containing entire payload of %Photoshop formated data (from APP13 Jpeg segment)
40+
/// @param sizePsData Size in bytes of pPsData.
41+
/// @param psTag %Tag number of the block to look for.
42+
/// @param record Output value that is set to the start of the data block within pPsData (may not be null).
43+
/// @param sizeHdr Output value that is set to the size of the header within the data block pointed to by record
44+
/// (may not be null).
45+
/// @param sizeData Output value that is set to the size of the actual data within the data block pointed to by record
46+
/// (may not be null).
47+
/// @return 0 if successful;<BR>
48+
/// 3 if no data for psTag was found in pPsData;<BR>
49+
/// -2 if the pPsData buffer does not contain valid data.
50+
static int locateIrb(const byte* pPsData, size_t sizePsData, uint16_t psTag, const byte** record,
51+
uint32_t* const sizeHdr, uint32_t* const sizeData);
52+
53+
/// @brief Forwards to locateIrb() with \em psTag = \em iptc_
54+
static int locateIptcIrb(const byte* pPsData, size_t sizePsData, const byte** record, uint32_t* const sizeHdr,
55+
uint32_t* const sizeData);
56+
57+
/// @brief Forwards to locatePreviewIrb() with \em psTag = \em preview_
58+
static int locatePreviewIrb(const byte* pPsData, size_t sizePsData, const byte** record, uint32_t* const sizeHdr,
59+
uint32_t* const sizeData);
60+
61+
/// @brief Set the new IPTC IRB, keeps existing IRBs but removes the IPTC block if there is no new IPTC data to write.
62+
/// @param pPsData Existing IRB buffer
63+
/// @param sizePsData Size of the IRB buffer, may be 0
64+
/// @param iptcData Iptc data to embed, may be empty
65+
/// @return A data buffer containing the new IRB buffer, may have 0 size
66+
static DataBuf setIptcIrb(const byte* pPsData, size_t sizePsData, const IptcData& iptcData);
67+
};
68+
} // namespace Exiv2
69+
70+
#endif // PHOTOSHOP_INT_HPP

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ set(PUBLIC_HEADERS
6565
../include/exiv2/mrwimage.hpp
6666
../include/exiv2/orfimage.hpp
6767
../include/exiv2/pgfimage.hpp
68+
../include/exiv2/photoshop.hpp
6869
../include/exiv2/preview.hpp
6970
../include/exiv2/properties.hpp
7071
../include/exiv2/psdimage.hpp
@@ -107,6 +108,7 @@ add_library( exiv2lib
107108
mrwimage.cpp
108109
orfimage.cpp
109110
pgfimage.cpp
111+
photoshop.cpp
110112
preview.cpp
111113
properties.cpp
112114
psdimage.cpp

src/image.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "xmpsidecar.hpp"
3939

4040
// + standard includes
41+
#include <array>
4142
#include <cstdio>
4243
#include <cstring>
4344
#include <limits>

0 commit comments

Comments
 (0)