Exiv2
types.hpp
Go to the documentation of this file.
1 // ***************************************************************** -*- C++ -*-
2 /*
3  * Copyright (C) 2004-2018 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  */
29 #ifndef TYPES_HPP_
30 #define TYPES_HPP_
31 
32 #include "exiv2lib_export.h"
33 
34 // included header files
35 #include "config.h"
36 #include "slice.hpp"
37 
38 // + standard includes
39 #include <string>
40 #include <vector>
41 #include <limits>
42 #include <algorithm>
43 #include <sstream>
44 
45 #ifdef _MSC_VER
46 // Visual Studio 2010 and later has stdint.h
47 # if _MSC_VER >= _MSC_VER_2010
48 # include <stdint.h>
49 # else
50 // Earlier compilers have MS C99 equivalents such as __int8
51  typedef unsigned __int8 uint8_t;
52  typedef unsigned __int16 uint16_t;
53  typedef unsigned __int32 uint32_t;
54  typedef unsigned __int64 uint64_t;
55  typedef __int8 int8_t;
56  typedef __int16 int16_t;
57  typedef __int32 int32_t;
58  typedef __int64 int64_t;
59 # endif
60 #else
61  #ifdef EXV_HAVE_STDINT_H
62  # include <stdint.h>
63  #endif
64 #endif
65 
66 
67 // MSVC macro to convert a string to a wide string
68 #ifdef EXV_UNICODE_PATH
69 # define EXV_WIDEN(t) L ## t
70 #endif
71 
77 #define EXV_CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
78 
79 // Simple min and max macros
81 #define EXV_MIN(a,b) ((a) < (b) ? (a) : (b))
82 #define EXV_MAX(a,b) ((a) > (b) ? (a) : (b))
84 
85 #if defined(__GNUC__) && (__GNUC__ >= 4) || defined(__clang__)
86 #define EXV_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
87 #elif defined(_MSC_VER) && (_MSC_VER >= 1700)
88 #define EXV_WARN_UNUSED_RESULT _Check_return_
89 #else
90 #define EXV_WARN_UNUSED_RESULT
91 #endif
92 
93 // *****************************************************************************
94 // forward declarations
95 struct tm;
96 
97 // *****************************************************************************
98 // namespace extensions
99 namespace Exiv2 {
100 
101 // *****************************************************************************
102 // type definitions
103 
105  typedef uint8_t byte;
106 
108  typedef std::pair<uint32_t, uint32_t> URational;
110  typedef std::pair<int32_t, int32_t> Rational;
111 
113  enum ByteOrder { invalidByteOrder, littleEndian, bigEndian };
114 
116  enum WriteMethod { wmIntrusive, wmNonIntrusive };
117 
119  enum MetadataId { mdNone=0, mdExif=1, mdIptc=2, mdComment=4, mdXmp=8, mdIccProfile=16 };
120 
122  enum AccessMode { amNone=0, amRead=1, amWrite=2, amReadWrite=3 };
123 
130  enum TypeId {
137  undefined = 7,
141  tiffFloat =11,
143  tiffIfd =13,
146  tiffIfd8 =18,
147  string =0x10000,
148  date =0x10001,
149  time =0x10002,
150  comment =0x10003,
151  directory =0x10004,
152  xmpText =0x10005,
153  xmpAlt =0x10006,
154  xmpBag =0x10007,
155  xmpSeq =0x10008,
156  langAlt =0x10009,
157  invalidTypeId =0x1fffe,
158  lastTypeId =0x1ffff
159  };
160 
162  typedef std::vector<byte> Blob;
163 
164 // *****************************************************************************
165 // class definitions
166 
168  class EXIV2API TypeInfo {
170  TypeInfo();
172  TypeInfo(const TypeInfo& rhs);
174  TypeInfo& operator=(const TypeInfo& rhs);
175 
176  public:
178  static const char* typeName(TypeId typeId);
180  static TypeId typeId(const std::string& typeName);
182  static long typeSize(TypeId typeId);
183 
184  };
185 
191  struct EXIV2API DataBufRef {
193  explicit DataBufRef(std::pair<byte*, long> rhs) : p(rhs) {}
195  std::pair<byte*, long> p;
196  };
197 
204  class EXIV2API DataBuf {
205  public:
207 
208  DataBuf();
211  explicit DataBuf(long size);
213  DataBuf(const byte* pData, long size);
219  DataBuf(DataBuf& rhs);
221  ~DataBuf();
223 
225 
226 
231  DataBuf& operator=(DataBuf& rhs);
237  void alloc(long size);
243  EXV_WARN_UNUSED_RESULT std::pair<byte*, long> release();
244 
248  void free();
249 
251  void reset(std::pair<byte*, long> =std::make_pair((byte*)(0),long(0)));
253 
262  DataBuf(const DataBufRef& rhs);
263  DataBuf& operator=(DataBufRef rhs);
264  operator DataBufRef();
266 
267  // DATA
271  long size_;
272  }; // class DataBuf
273 
285  EXIV2API Slice<byte*> makeSlice(DataBuf& buf, size_t begin, size_t end);
286 
288  EXIV2API Slice<const byte*> makeSlice(const DataBuf& buf, size_t begin, size_t end);
289 
290 // *****************************************************************************
291 // free functions
292 
294  EXIV2API uint16_t getUShort(const byte* buf, ByteOrder byteOrder);
296  template <typename T>
297  uint16_t getUShort(const Slice<T>& buf, ByteOrder byteOrder)
298  {
299  if (byteOrder == littleEndian) {
300  return static_cast<byte>(buf.at(1)) << 8 | static_cast<byte>(buf.at(0));
301  } else {
302  return static_cast<byte>(buf.at(0)) << 8 | static_cast<byte>(buf.at(1));
303  }
304  }
305 
307  EXIV2API uint32_t getULong(const byte* buf, ByteOrder byteOrder);
309  EXIV2API uint64_t getULongLong(const byte* buf, ByteOrder byteOrder);
311  EXIV2API URational getURational(const byte* buf, ByteOrder byteOrder);
313  EXIV2API int16_t getShort(const byte* buf, ByteOrder byteOrder);
315  EXIV2API int32_t getLong(const byte* buf, ByteOrder byteOrder);
317  EXIV2API Rational getRational(const byte* buf, ByteOrder byteOrder);
319  EXIV2API float getFloat(const byte* buf, ByteOrder byteOrder);
321  EXIV2API double getDouble(const byte* buf, ByteOrder byteOrder);
322 
324  EXIV2API std::ostream& operator<<(std::ostream& os, const Rational& r);
326  EXIV2API std::istream& operator>>(std::istream& is, Rational& r);
328  EXIV2API std::ostream& operator<<(std::ostream& os, const URational& r);
330  EXIV2API std::istream& operator>>(std::istream& is, URational& r);
331 
336  EXIV2API long us2Data(byte* buf, uint16_t s, ByteOrder byteOrder);
341  EXIV2API long ul2Data(byte* buf, uint32_t l, ByteOrder byteOrder);
346  EXIV2API long ur2Data(byte* buf, URational l, ByteOrder byteOrder);
351  EXIV2API long s2Data(byte* buf, int16_t s, ByteOrder byteOrder);
356  EXIV2API long l2Data(byte* buf, int32_t l, ByteOrder byteOrder);
361  EXIV2API long r2Data(byte* buf, Rational l, ByteOrder byteOrder);
366  EXIV2API long f2Data(byte* buf, float f, ByteOrder byteOrder);
371  EXIV2API long d2Data(byte* buf, double d, ByteOrder byteOrder);
372 
378  EXIV2API void hexdump(std::ostream& os, const byte* buf, long len, long offset =0);
379 
385  EXIV2API bool isHex(const std::string& str,
386  size_t size =0,
387  const std::string& prefix ="");
388 
394  EXIV2API int exifTime(const char* buf, struct tm* tm);
395 
400  EXIV2API const char* exvGettext(const char* str);
401 
402 #ifdef EXV_UNICODE_PATH
403  EXIV2API std::wstring s2ws(const std::string& s);
406  EXIV2API std::string ws2s(const std::wstring& s);
407 #endif
408 
420  EXIV2API long parseLong(const std::string& s, bool& ok);
421 
434  EXIV2API float parseFloat(const std::string& s, bool& ok);
435 
450  EXIV2API Rational parseRational(const std::string& s, bool& ok);
451 
458  EXIV2API Rational floatToRationalCast(float f);
459 
460 // *****************************************************************************
461 // template and inline definitions
462 
507  template<typename T, typename K, int N>
508  const T* find(T (&src)[N], const K& key)
509  {
510  const T* rc = std::find(src, src + N, key);
511  return rc == src + N ? 0 : rc;
512  }
513 
515  template <typename T, int N> char (&sizer(T (&)[N]))[N];
517 #define EXV_COUNTOF(a) (sizeof(Exiv2::sizer(a)))
518 
520  template<typename T>
521  std::string toString(const T& arg)
522  {
523  std::ostringstream os;
524  os << arg;
525  return os.str();
526  }
527 
539  template<typename T>
540  T stringTo(const std::string& s, bool& ok)
541  {
542  std::istringstream is(s);
543  T tmp;
544  ok = (is >> tmp) ? true : false;
545  std::string rest;
546  is >> std::skipws >> rest;
547  if (!rest.empty()) ok = false;
548  return tmp;
549  }
550 
558  template<>
559  bool stringTo<bool>(const std::string& s, bool& ok);
560 
569  template <typename IntType>
570  IntType gcd(IntType n, IntType m)
571  {
572  // Avoid repeated construction
573  IntType zero(0);
574 
575  // This is abs() - given the existence of broken compilers with Koenig
576  // lookup issues and other problems, I code this explicitly. (Remember,
577  // IntType may be a user-defined type).
578 #ifdef _MSC_VER
579 #pragma warning( disable : 4146 )
580 #undef max
581 #undef min
582 #endif
583  if (n < zero) {
584  if (n == std::numeric_limits<IntType>::min()) {
585  n = std::numeric_limits<IntType>::max();
586  } else {
587  n = -n;
588  }
589  }
590  if (m < zero)
591  m = -m;
592 #ifdef _MSC_VER
593 #pragma warning( default : 4146 )
594 #endif
595 
596  // As n and m are now positive, we can be sure that %= returns a
597  // positive value (the standard guarantees this for built-in types,
598  // and we require it of user-defined types).
599  for(;;) {
600  if(m == zero)
601  return n;
602  n %= m;
603  if(n == zero)
604  return m;
605  m %= n;
606  }
607  }
608 
609 } // namespace Exiv2
610 
611 #endif // #ifndef TYPES_HPP_
Exif SHORT type, 16-bit (2-byte) unsigned integer.
Definition: types.hpp:133
Exif SLONG type, a 32-bit (4-byte) signed (twos-complement) integer.
Definition: types.hpp:139
EXIV2API Rational floatToRationalCast(float f)
Very simple conversion of a float to a Rational.
Definition: types.cpp:694
Slice (= view) for STL containers.
Definition: slice.hpp:529
Auxiliary type to enable copies and assignments, similar to std::auto_ptr_ref. See http://www...
Definition: types.hpp:191
Exif SRATIONAL type, two SLONGs: numerator and denumerator of a fraction.
Definition: types.hpp:140
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:113
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:204
IPTC time type.
Definition: types.hpp:149
std::pair< int32_t, int32_t > Rational
8 byte signed rational type.
Definition: types.hpp:110
long size_
The current size of the buffer.
Definition: types.hpp:271
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:445
bool stringTo< bool >(const std::string &s, bool &ok)
Specialization of stringTo(const std::string& s, bool& ok) for bool.
Definition: types.cpp:612
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:130
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:361
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:483
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:462
EXIV2API int16_t getShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte signed short value from the data buffer.
Definition: types.cpp:318
uint8_t byte
1 byte unsigned integer type.
Definition: types.hpp:105
Invalid type id.
Definition: types.hpp:157
MetadataId
An identifier for each type of metadata.
Definition: types.hpp:119
std::string toString(const T &arg)
Utility function to convert the argument of any type to a string.
Definition: types.hpp:521
EXIV2API Rational parseRational(const std::string &s, bool &ok)
Return a Rational set to the value represented by s.
Definition: types.cpp:676
Exif UNDEFINED type, an 8-bit byte that may contain anything.
Definition: types.hpp:137
char(& sizer(T(&)[N]))[N]
Template used in the COUNTOF macro to determine the size of an array.
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:432
XMP bag type.
Definition: types.hpp:154
WriteMethod
Type to indicate write method used by TIFF parsers.
Definition: types.hpp:116
DataBufRef(std::pair< byte *, long > rhs)
Constructor.
Definition: types.hpp:193
Exif LONG type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:134
const T * find(T(&src)[N], const K &key)
Find an element that matches key in the array src.
Definition: types.hpp:508
TIFF IFD type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:143
EXIV2API URational getURational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte unsigned rational value from the data buffer.
Definition: types.cpp:311
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:469
EXIV2API std::istream & operator>>(std::istream &is, Rational &r)
Input operator for our fake rational.
Definition: types.cpp:228
Exif SBYTE type, an 8-bit signed (twos-complement) integer.
Definition: types.hpp:136
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:576
Exif RATIONAL type, two LONGs: numerator and denumerator of a fraction.
Definition: types.hpp:135
Exif ASCII type, 8-bit byte.
Definition: types.hpp:132
TIFF DOUBLE type, double precision (8-byte) IEEE format.
Definition: types.hpp:142
byte * pData_
Pointer to the buffer, 0 if none has been allocated.
Definition: types.hpp:269
TIFF IFD type, 64-bit (8-byte) unsigned integer.
Definition: types.hpp:146
Exif LONG LONG type, 64-bit (8-byte) signed integer.
Definition: types.hpp:145
Exif BYTE type, 8-bit unsigned integer.
Definition: types.hpp:131
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:408
Exif SSHORT type, a 16-bit (2-byte) signed (twos-complement) integer.
Definition: types.hpp:138
AccessMode
An identifier for each mode of metadata support.
Definition: types.hpp:122
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, false otherwise. If size is 0, any number of digits is allowed and all are checked.
Definition: types.cpp:543
EXIV2API float parseFloat(const std::string &s, bool &ok)
Return a float set to the value represented by s.
Definition: types.cpp:655
std::vector< byte > Blob
Container for binary data.
Definition: types.hpp:162
TIFF FLOAT type, single precision (4-byte) IEEE format.
Definition: types.hpp:141
std::pair< uint32_t, uint32_t > URational
8 byte unsigned rational type.
Definition: types.hpp:108
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:395
Slice< T > makeSlice(T &cont, size_t begin, size_t end)
Return a new slice with the given bounds.
Definition: slice.hpp:673
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:425
EXIV2API int32_t getLong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte signed long value from the data buffer.
Definition: types.cpp:328
EXIV2API uint32_t getULong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte unsigned long value from the data buffer.
Definition: types.cpp:283
IPTC string type.
Definition: types.hpp:147
XMP text type.
Definition: types.hpp:152
std::pair< byte *, long > p
Pointer to a byte array and its size.
Definition: types.hpp:195
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:518
EXIV2API long parseLong(const std::string &s, bool &ok)
Return a long set to the value represented by s.
Definition: types.cpp:631
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
Simple implementation of slices (=views) for STL containers and C-arrays.
T stringTo(const std::string &s, bool &ok)
Utility function to convert a string to a value of type T.
Definition: types.hpp:540
value_type & at(size_t index)
Definition: slice.hpp:269
EXIV2API std::ostream & operator<<(std::ostream &os, const DataSet &dataSet)
Output operator for dataSet.
Definition: datasets.cpp:709
XMP sequence type.
Definition: types.hpp:155
Exiv2 type for the Exif user comment.
Definition: types.hpp:150
EXIV2API uint64_t getULongLong(const byte *buf, ByteOrder byteOrder)
Read a 8 byte unsigned long value from the data buffer.
Definition: types.cpp:295
IPTC date type.
Definition: types.hpp:148
IntType gcd(IntType n, IntType m)
Return the greatest common denominator of n and m. (Implementation from Boost rational.hpp)
Definition: types.hpp:570
XMP language alternative type.
Definition: types.hpp:156
EXIV2API Rational getRational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte signed rational value from the data buffer.
Definition: types.cpp:340
Type information lookup functions. Implemented as a static class.
Definition: types.hpp:168
Exiv2 type for a CIFF directory.
Definition: types.hpp:151
Exif LONG LONG type, 64-bit (8-byte) unsigned integer.
Definition: types.hpp:144
XMP alternative type.
Definition: types.hpp:153
Last type id.
Definition: types.hpp:158
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:347
EXIV2API uint16_t getUShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte unsigned short value from the data buffer.
Definition: types.cpp:278
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:556