Exiv2
types.hpp
1// ***************************************************************** -*- C++ -*-
2/*
3 * Copyright (C) 2004-2021 Exiv2 authors
4 * This program is part of the Exiv2 distribution.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
19 */
20#ifndef TYPES_HPP_
21#define TYPES_HPP_
22
23#include "exiv2lib_export.h"
24
25// included header files
26#include "config.h"
27#include "slice.hpp"
28
29// + standard includes
30#include <cstdint>
31#include <string>
32#include <vector>
33#include <limits>
34#include <algorithm>
35#include <sstream>
36
37// MSVC macro to convert a string to a wide string
38#ifdef EXV_UNICODE_PATH
39# define EXV_WIDEN(t) L ## t
40#endif
41
47#define EXV_CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
48
49#if defined(__GNUC__) && (__GNUC__ >= 4) || defined(__clang__)
50#define EXV_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
51#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
52#define EXV_WARN_UNUSED_RESULT _Check_return_
53#else
54#define EXV_WARN_UNUSED_RESULT
55#endif
56
57#ifndef _MSC_VER
58#define EXV_UNUSED [[gnu::unused]]
59#else
60#define EXV_UNUSED
61#endif
62
63// *****************************************************************************
64// forward declarations
65struct tm;
66
67// *****************************************************************************
68// namespace extensions
69namespace Exiv2 {
70
71// *****************************************************************************
72// type definitions
73
75 typedef uint8_t byte;
76
78 typedef std::pair<uint32_t, uint32_t> URational;
80 typedef std::pair<int32_t, int32_t> Rational;
81
83 enum ByteOrder { invalidByteOrder, littleEndian, bigEndian };
84
86 enum WriteMethod { wmIntrusive, wmNonIntrusive };
87
89 enum MetadataId { mdNone=0, mdExif=1, mdIptc=2, mdComment=4, mdXmp=8, mdIccProfile=16 };
90
92 enum AccessMode { amNone=0, amRead=1, amWrite=2, amReadWrite=3 };
93
100 enum TypeId {
117 string =0x10000,
118 date =0x10001,
119 time =0x10002,
120 comment =0x10003,
121 directory =0x10004,
122 xmpText =0x10005,
123 xmpAlt =0x10006,
124 xmpBag =0x10007,
125 xmpSeq =0x10008,
126 langAlt =0x10009,
127 invalidTypeId =0x1fffe,
128 lastTypeId =0x1ffff
129 };
130
132 typedef std::vector<byte> Blob;
133
134// *****************************************************************************
135// class definitions
136
138 class EXIV2API TypeInfo {
139 public:
141 TypeInfo() = delete;
143 TypeInfo(const TypeInfo& rhs) = delete;
145 TypeInfo& operator=(const TypeInfo& rhs) = delete;
146
148 static const char* typeName(TypeId typeId);
150 static TypeId typeId(const std::string& typeName);
152 static long typeSize(TypeId typeId);
153
154 };
155
162 struct EXIV2API DataBuf {
164
165
166 DataBuf();
168 explicit DataBuf(long size);
170 DataBuf(const byte* pData, long size);
174 DataBuf(const DataBuf& rhs);
180 DataBuf(DataBuf&& rhs);
182 ~DataBuf();
184
186
187
192 DataBuf& operator=(DataBuf&& rhs);
193
194 // No copy assignment.
195 DataBuf& operator=(const DataBuf&) = delete;
196
202 void alloc(long size);
206 void resize(long size);
207
213 EXV_WARN_UNUSED_RESULT std::pair<byte*, long> release();
214
216 void reset();
218
220 void clear();
221
222 long size() const { return size_; }
223
224 uint8_t read_uint8(size_t offset) const;
225 void write_uint8(size_t offset, uint8_t x);
226
227 uint16_t read_uint16(size_t offset, ByteOrder byteOrder) const;
228 void write_uint16(size_t offset, uint16_t x, ByteOrder byteOrder);
229
230 uint32_t read_uint32(size_t offset, ByteOrder byteOrder) const;
231 void write_uint32(size_t offset, uint32_t x, ByteOrder byteOrder);
232
233 uint64_t read_uint64(size_t offset, ByteOrder byteOrder) const;
234 void write_uint64(size_t offset, uint64_t x, ByteOrder byteOrder);
235
237 void copyBytes(size_t offset, const void* buf, size_t bufsize);
238
240 int cmpBytes(size_t offset, const void* buf, size_t bufsize) const;
241
243 byte* data(size_t offset = 0);
244
246 const byte* c_data(size_t offset = 0) const;
247
249 const char* c_str(size_t offset = 0) const;
250
251 private:
252 // DATA
254 byte* pData_;
256 long size_;
257 }; // class DataBuf
258
270 EXIV2API Slice<byte*> makeSlice(DataBuf& buf, size_t begin, size_t end);
271
273 EXIV2API Slice<const byte*> makeSlice(const DataBuf& buf, size_t begin, size_t end);
274
275// *****************************************************************************
276// free functions
277
279 EXIV2API uint16_t getUShort(const byte* buf, ByteOrder byteOrder);
281 template <typename T>
282 uint16_t getUShort(const Slice<T>& buf, ByteOrder byteOrder)
283 {
284 if (byteOrder == littleEndian) {
285 return static_cast<byte>(buf.at(1)) << 8 | static_cast<byte>(buf.at(0));
286 }
287 return static_cast<byte>(buf.at(0)) << 8 | static_cast<byte>(buf.at(1));
288 }
289
291 EXIV2API uint32_t getULong(const byte* buf, ByteOrder byteOrder);
293 EXIV2API uint64_t getULongLong(const byte* buf, ByteOrder byteOrder);
295 EXIV2API URational getURational(const byte* buf, ByteOrder byteOrder);
297 EXIV2API int16_t getShort(const byte* buf, ByteOrder byteOrder);
299 EXIV2API int32_t getLong(const byte* buf, ByteOrder byteOrder);
301 EXIV2API Rational getRational(const byte* buf, ByteOrder byteOrder);
303 EXIV2API float getFloat(const byte* buf, ByteOrder byteOrder);
305 EXIV2API double getDouble(const byte* buf, ByteOrder byteOrder);
306
308 EXIV2API std::ostream& operator<<(std::ostream& os, const Rational& r);
310 EXIV2API std::istream& operator>>(std::istream& is, Rational& r);
312 EXIV2API std::ostream& operator<<(std::ostream& os, const URational& r);
314 EXIV2API std::istream& operator>>(std::istream& is, URational& r);
315
320 EXIV2API long us2Data(byte* buf, uint16_t s, ByteOrder byteOrder);
325 EXIV2API long ul2Data(byte* buf, uint32_t l, ByteOrder byteOrder);
330 EXIV2API long ull2Data(byte* buf, uint64_t l, ByteOrder byteOrder);
335 EXIV2API long ur2Data(byte* buf, URational l, ByteOrder byteOrder);
340 EXIV2API long s2Data(byte* buf, int16_t s, ByteOrder byteOrder);
345 EXIV2API long l2Data(byte* buf, int32_t l, ByteOrder byteOrder);
350 EXIV2API long r2Data(byte* buf, Rational l, ByteOrder byteOrder);
355 EXIV2API long f2Data(byte* buf, float f, ByteOrder byteOrder);
360 EXIV2API long d2Data(byte* buf, double d, ByteOrder byteOrder);
361
367 EXIV2API void hexdump(std::ostream& os, const byte* buf, long len, long offset =0);
368
374 EXIV2API bool isHex(const std::string& str,
375 size_t size =0,
376 const std::string& prefix ="");
377
383 EXIV2API int exifTime(const char* buf, struct tm* tm);
384
389 EXIV2API const char* exvGettext(const char* str);
390
391#ifdef EXV_UNICODE_PATH
393 EXIV2API std::wstring s2ws(const std::string& s);
395 EXIV2API std::string ws2s(const std::wstring& s);
396#endif
409 EXIV2API long parseLong(const std::string& s, bool& ok);
410
423 EXIV2API float parseFloat(const std::string& s, bool& ok);
424
439 EXIV2API Rational parseRational(const std::string& s, bool& ok);
440
447 EXIV2API Rational floatToRationalCast(float f);
448
449// *****************************************************************************
450// template and inline definitions
451
496 template<typename T, typename K, int N>
497 const T* find(T (&src)[N], const K& key)
498 {
499 const T* rc = std::find(src, src + N, key);
500 return rc == src + N ? 0 : rc;
501 }
502
504 template <typename T, int N> char (&sizer(T (&)[N]))[N];
506#define EXV_COUNTOF(a) (sizeof(Exiv2::sizer(a)))
507
509 template<typename T>
510 std::string toString(const T& arg)
511 {
512 std::ostringstream os;
513 os << arg;
514 return os.str();
515 }
516
528 template<typename T>
529 T stringTo(const std::string& s, bool& ok)
530 {
531 std::istringstream is(s);
532 T tmp = T();
533 ok = bool(is >> tmp);
534 std::string rest;
535 is >> std::skipws >> rest;
536 if (!rest.empty()) ok = false;
537 return tmp;
538 }
539
547 template<>
548 bool stringTo<bool>(const std::string& s, bool& ok);
549
558 template <typename IntType>
559 IntType gcd(IntType n, IntType m)
560 {
561 // Avoid repeated construction
562 IntType zero(0);
563
564 // This is abs() - given the existence of broken compilers with Koenig
565 // lookup issues and other problems, I code this explicitly. (Remember,
566 // IntType may be a user-defined type).
567#ifdef _MSC_VER
568#pragma warning( disable : 4146 )
569#undef max
570#undef min
571#endif
572 if (n < zero) {
573 if (n == std::numeric_limits<IntType>::min()) {
574 n = std::numeric_limits<IntType>::max();
575 } else {
576 n = -n;
577 }
578 }
579 if (m < zero)
580 m = -m;
581#ifdef _MSC_VER
582#pragma warning( default : 4146 )
583#endif
584
585 // As n and m are now positive, we can be sure that %= returns a
586 // positive value (the standard guarantees this for built-in types,
587 // and we require it of user-defined types).
588 for(;;) {
589 if(m == zero)
590 return n;
591 n %= m;
592 if(n == zero)
593 return m;
594 m %= n;
595 }
596 }
597
598} // namespace Exiv2
599
600#endif // #ifndef TYPES_HPP_
Type information lookup functions. Implemented as a static class.
Definition: types.hpp:138
TypeInfo & operator=(const TypeInfo &rhs)=delete
Prevent assignment: not implemented.
TypeInfo(const TypeInfo &rhs)=delete
Prevent copy-construction: not implemented.
TypeInfo()=delete
Prevent construction: not implemented.
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: basicio.hpp:43
EXIV2API std::ostream & operator<<(std::ostream &os, const DataSet &dataSet)
Output operator for dataSet.
Definition: datasets.cpp:677
EXIV2API double getDouble(const byte *buf, ByteOrder byteOrder)
Read an 8 byte double precision floating point value (IEEE 754 binary64) from the data buffer.
Definition: types.cpp:440
EXIV2API void hexdump(std::ostream &os, const byte *buf, long len, long offset=0)
Print len bytes from buf in hex and ASCII format to the given stream, prefixed with the position in t...
Definition: types.cpp:614
EXIV2API long f2Data(byte *buf, float f, ByteOrder byteOrder)
Convert a single precision floating point (IEEE 754 binary32) float to data, write the data to the bu...
Definition: types.cpp:565
const T * find(T(&src)[N], const K &key)
Find an element that matches key in the array src.
Definition: types.hpp:497
EXIV2API long d2Data(byte *buf, double d, ByteOrder byteOrder)
Convert a double precision floating point (IEEE 754 binary64) double to data, write the data to the b...
Definition: types.cpp:579
uint8_t byte
1 byte unsigned integer type.
Definition: types.hpp:75
EXIV2API long parseLong(const std::string &s, bool &ok)
Return a long set to the value represented by s.
Definition: types.cpp:726
EXIV2API float getFloat(const byte *buf, ByteOrder byteOrder)
Read a 4 byte single precision floating point value (IEEE 754 binary32) from the data buffer.
Definition: types.cpp:426
EXIV2API float parseFloat(const std::string &s, bool &ok)
Return a float set to the value represented by s.
Definition: types.cpp:750
EXIV2API bool isHex(const std::string &str, size_t size=0, const std::string &prefix="")
Return true if str is a hex number starting with prefix followed by size hex digits,...
Definition: types.cpp:638
EXIV2API Rational floatToRationalCast(float f)
Very simple conversion of a float to a Rational.
Definition: types.cpp:792
EXIV2API long l2Data(byte *buf, int32_t l, ByteOrder byteOrder)
Convert a signed long to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:541
IntType gcd(IntType n, IntType m)
Return the greatest common denominator of n and m. (Implementation from Boost rational....
Definition: types.hpp:559
EXIV2API const char * exvGettext(const char *str)
Translate a string using the gettext framework. This wrapper hides all the implementation details fro...
Definition: types.cpp:671
EXIV2API int16_t getShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte signed short value from the data buffer.
Definition: types.cpp:403
EXIV2API uint32_t getULong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte unsigned long value from the data buffer.
Definition: types.cpp:374
EXIV2API long ul2Data(byte *buf, uint32_t l, ByteOrder byteOrder)
Convert an unsigned long to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:487
Slice< T > makeSlice(T &cont, size_t begin, size_t end)
Return a new slice with the given bounds.
Definition: slice.hpp:665
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:100
@ unsignedShort
Exif SHORT type, 16-bit (2-byte) unsigned integer.
Definition: types.hpp:103
@ date
IPTC date type.
Definition: types.hpp:118
@ signedRational
Exif SRATIONAL type, two SLONGs: numerator and denumerator of a fraction.
Definition: types.hpp:110
@ tiffIfd8
TIFF IFD type, 64-bit (8-byte) unsigned integer.
Definition: types.hpp:116
@ lastTypeId
Last type id.
Definition: types.hpp:128
@ unsignedLongLong
Exif LONG LONG type, 64-bit (8-byte) unsigned integer.
Definition: types.hpp:114
@ unsignedLong
Exif LONG type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:104
@ signedShort
Exif SSHORT type, a 16-bit (2-byte) signed (twos-complement) integer.
Definition: types.hpp:108
@ signedLongLong
Exif LONG LONG type, 64-bit (8-byte) signed integer.
Definition: types.hpp:115
@ signedLong
Exif SLONG type, a 32-bit (4-byte) signed (twos-complement) integer.
Definition: types.hpp:109
@ langAlt
XMP language alternative type.
Definition: types.hpp:126
@ xmpAlt
XMP alternative type.
Definition: types.hpp:123
@ unsignedByte
Exif BYTE type, 8-bit unsigned integer.
Definition: types.hpp:101
@ signedByte
Exif SBYTE type, an 8-bit signed (twos-complement) integer.
Definition: types.hpp:106
@ asciiString
Exif ASCII type, 8-bit byte.
Definition: types.hpp:102
@ xmpText
XMP text type.
Definition: types.hpp:122
@ time
IPTC time type.
Definition: types.hpp:119
@ xmpSeq
XMP sequence type.
Definition: types.hpp:125
@ comment
Exiv2 type for the Exif user comment.
Definition: types.hpp:120
@ tiffDouble
TIFF DOUBLE type, double precision (8-byte) IEEE format.
Definition: types.hpp:112
@ undefined
Exif UNDEFINED type, an 8-bit byte that may contain anything.
Definition: types.hpp:107
@ xmpBag
XMP bag type.
Definition: types.hpp:124
@ tiffFloat
TIFF FLOAT type, single precision (4-byte) IEEE format.
Definition: types.hpp:111
@ unsignedRational
Exif RATIONAL type, two LONGs: numerator and denumerator of a fraction.
Definition: types.hpp:105
@ tiffIfd
TIFF IFD type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:113
@ invalidTypeId
Invalid type id.
Definition: types.hpp:127
@ directory
Exiv2 type for a CIFF directory.
Definition: types.hpp:121
bool stringTo< bool >(const std::string &s, bool &ok)
Specialization of stringTo(const std::string& s, bool& ok) for bool.
Definition: types.cpp:707
EXIV2API uint16_t getUShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte unsigned short value from the data buffer.
Definition: types.cpp:369
EXIV2API Rational parseRational(const std::string &s, bool &ok)
Return a Rational set to the value represented by s.
Definition: types.cpp:772
std::vector< byte > Blob
Container for binary data.
Definition: types.hpp:132
char(& sizer(T(&)[N]))[N]
Template used in the COUNTOF macro to determine the size of an array.
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:83
T stringTo(const std::string &s, bool &ok)
Utility function to convert a string to a value of type T.
Definition: types.hpp:529
std::pair< int32_t, int32_t > Rational
8 byte signed rational type.
Definition: types.hpp:80
EXIV2API uint64_t getULongLong(const byte *buf, ByteOrder byteOrder)
Read a 8 byte unsigned long value from the data buffer.
Definition: types.cpp:382
WriteMethod
Type to indicate write method used by TIFF parsers.
Definition: types.hpp:86
EXIV2API Rational getRational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte signed rational value from the data buffer.
Definition: types.cpp:419
EXIV2API long ull2Data(byte *buf, uint64_t l, ByteOrder byteOrder)
Convert an uint64_t to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:504
EXIV2API long r2Data(byte *buf, Rational l, ByteOrder byteOrder)
Convert a signed rational to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:558
MetadataId
An identifier for each type of metadata.
Definition: types.hpp:89
EXIV2API long ur2Data(byte *buf, URational l, ByteOrder byteOrder)
Convert an unsigned rational to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:521
EXIV2API std::istream & operator>>(std::istream &is, Rational &r)
Input operator for our fake rational.
Definition: types.cpp:319
EXIV2API int32_t getLong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte signed long value from the data buffer.
Definition: types.cpp:411
EXIV2API long s2Data(byte *buf, int16_t s, ByteOrder byteOrder)
Convert a signed short to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:528
std::string toString(const T &arg)
Utility function to convert the argument of any type to a string.
Definition: types.hpp:510
std::pair< uint32_t, uint32_t > URational
8 byte unsigned rational type.
Definition: types.hpp:78
EXIV2API URational getURational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte unsigned rational value from the data buffer.
Definition: types.cpp:396
AccessMode
An identifier for each mode of metadata support.
Definition: types.hpp:92
EXIV2API int exifTime(const char *buf, struct tm *tm)
Converts a string in the form "%Y:%m:%d %H:%M:%S", e.g., "2007:05:24 12:31:55" to broken down time fo...
Definition: types.cpp:651
EXIV2API long us2Data(byte *buf, uint16_t s, ByteOrder byteOrder)
Convert an unsigned short to data, write the data to the buffer, return number of bytes written.
Definition: types.cpp:474
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:162
value_type & at(size_t index)
Definition: slice.hpp:261
Slice (= view) for STL containers.
Definition: slice.hpp:522