types.hpp
// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004-2021 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
#ifndef TYPES_HPP_
#define TYPES_HPP_
#include "exiv2lib_export.h"
// included header files
#include "config.h"
#include "slice.hpp"
// + standard includes
#include <string>
#include <vector>
#include <limits>
#include <algorithm>
#include <sstream>
#ifdef _MSC_VER
// Visual Studio 2010 and later has stdint.h
# if _MSC_VER >= _MSC_VER_2010
# include <stdint.h>
# else
// Earlier compilers have MS C99 equivalents such as __int8
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
# endif
#else
# include <stdint.h>
#endif
// MSVC macro to convert a string to a wide string
#ifdef EXV_UNICODE_PATH
# define EXV_WIDEN(t) L ## t
#endif
#define EXV_CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
// Simple min and max macros
#define EXV_MIN(a,b) ((a) < (b) ? (a) : (b))
#define EXV_MAX(a,b) ((a) > (b) ? (a) : (b))
#if defined(__GNUC__) && (__GNUC__ >= 4) || defined(__clang__)
#define EXV_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
#define EXV_WARN_UNUSED_RESULT _Check_return_
#else
#define EXV_WARN_UNUSED_RESULT
#endif
// *****************************************************************************
// forward declarations
struct tm;
// *****************************************************************************
// namespace extensions
namespace Exiv2 {
// *****************************************************************************
// type definitions
typedef uint8_t byte;
typedef std::pair<uint32_t, uint32_t> URational;
typedef std::pair<int32_t, int32_t> Rational;
enum ByteOrder { invalidByteOrder, littleEndian, bigEndian };
enum WriteMethod { wmIntrusive, wmNonIntrusive };
enum MetadataId { mdNone=0, mdExif=1, mdIptc=2, mdComment=4, mdXmp=8, mdIccProfile=16 };
enum AccessMode { amNone=0, amRead=1, amWrite=2, amReadWrite=3 };
enum TypeId {
unsignedByte = 1,
asciiString = 2,
unsignedShort = 3,
unsignedLong = 4,
unsignedRational = 5,
signedByte = 6,
undefined = 7,
signedShort = 8,
signedLong = 9,
signedRational =10,
tiffFloat =11,
tiffDouble =12,
tiffIfd =13,
unsignedLongLong =16,
signedLongLong =17,
tiffIfd8 =18,
string =0x10000,
date =0x10001,
time =0x10002,
comment =0x10003,
directory =0x10004,
xmpText =0x10005,
xmpAlt =0x10006,
xmpBag =0x10007,
xmpSeq =0x10008,
langAlt =0x10009,
invalidTypeId =0x1fffe,
lastTypeId =0x1ffff
};
typedef std::vector<byte> Blob;
// *****************************************************************************
// class definitions
class EXIV2API TypeInfo {
TypeInfo();
TypeInfo(const TypeInfo& rhs);
TypeInfo& operator=(const TypeInfo& rhs);
public:
static const char* typeName(TypeId typeId);
static TypeId typeId(const std::string& typeName);
static long typeSize(TypeId typeId);
};
struct EXIV2API DataBufRef {
explicit DataBufRef(std::pair<byte*, long> rhs) : p(rhs) {}
std::pair<byte*, long> p;
};
class EXIV2API DataBuf {
public:
DataBuf();
explicit DataBuf(long size);
DataBuf(const byte* pData, long size);
DataBuf(DataBuf& rhs);
~DataBuf();
DataBuf& operator=(DataBuf& rhs);
void alloc(long size);
EXV_WARN_UNUSED_RESULT std::pair<byte*, long> release();
void free();
void reset(std::pair<byte*, long> =std::make_pair((byte*)(0),long(0)));
DataBuf(const DataBufRef& rhs);
DataBuf& operator=(DataBufRef rhs);
operator DataBufRef();
// DATA
byte* pData_;
long size_;
}; // class DataBuf
EXIV2API Slice<byte*> makeSlice(DataBuf& buf, size_t begin, size_t end);
EXIV2API Slice<const byte*> makeSlice(const DataBuf& buf, size_t begin, size_t end);
// *****************************************************************************
// free functions
EXIV2API uint16_t getUShort(const byte* buf, ByteOrder byteOrder);
template <typename T>
uint16_t getUShort(const Slice<T>& buf, ByteOrder byteOrder)
{
if (byteOrder == littleEndian) {
return static_cast<byte>(buf.at(1)) << 8 | static_cast<byte>(buf.at(0));
} else {
return static_cast<byte>(buf.at(0)) << 8 | static_cast<byte>(buf.at(1));
}
}
EXIV2API uint32_t getULong(const byte* buf, ByteOrder byteOrder);
EXIV2API uint64_t getULongLong(const byte* buf, ByteOrder byteOrder);
EXIV2API URational getURational(const byte* buf, ByteOrder byteOrder);
EXIV2API int16_t getShort(const byte* buf, ByteOrder byteOrder);
EXIV2API int32_t getLong(const byte* buf, ByteOrder byteOrder);
EXIV2API Rational getRational(const byte* buf, ByteOrder byteOrder);
EXIV2API float getFloat(const byte* buf, ByteOrder byteOrder);
EXIV2API double getDouble(const byte* buf, ByteOrder byteOrder);
EXIV2API std::ostream& operator<<(std::ostream& os, const Rational& r);
EXIV2API std::istream& operator>>(std::istream& is, Rational& r);
EXIV2API std::ostream& operator<<(std::ostream& os, const URational& r);
EXIV2API std::istream& operator>>(std::istream& is, URational& r);
EXIV2API long us2Data(byte* buf, uint16_t s, ByteOrder byteOrder);
EXIV2API long ul2Data(byte* buf, uint32_t l, ByteOrder byteOrder);
EXIV2API long ur2Data(byte* buf, URational l, ByteOrder byteOrder);
EXIV2API long s2Data(byte* buf, int16_t s, ByteOrder byteOrder);
EXIV2API long l2Data(byte* buf, int32_t l, ByteOrder byteOrder);
EXIV2API long r2Data(byte* buf, Rational l, ByteOrder byteOrder);
EXIV2API long f2Data(byte* buf, float f, ByteOrder byteOrder);
EXIV2API long d2Data(byte* buf, double d, ByteOrder byteOrder);
EXIV2API void hexdump(std::ostream& os, const byte* buf, long len, long offset =0);
EXIV2API bool isHex(const std::string& str,
size_t size =0,
const std::string& prefix ="");
EXIV2API int exifTime(const char* buf, struct tm* tm);
EXIV2API const char* exvGettext(const char* str);
#ifdef EXV_UNICODE_PATH
EXIV2API std::wstring s2ws(const std::string& s);
EXIV2API std::string ws2s(const std::wstring& s);
#endif
EXIV2API long parseLong(const std::string& s, bool& ok);
EXIV2API float parseFloat(const std::string& s, bool& ok);
EXIV2API Rational parseRational(const std::string& s, bool& ok);
EXIV2API Rational floatToRationalCast(float f);
// *****************************************************************************
// template and inline definitions
template<typename T, typename K, int N>
const T* find(T (&src)[N], const K& key)
{
const T* rc = std::find(src, src + N, key);
return rc == src + N ? 0 : rc;
}
template <typename T, int N> char (&sizer(T (&)[N]))[N];
#define EXV_COUNTOF(a) (sizeof(Exiv2::sizer(a)))
template<typename T>
std::string toString(const T& arg)
{
std::ostringstream os;
os << arg;
return os.str();
}
template<typename T>
T stringTo(const std::string& s, bool& ok)
{
std::istringstream is(s);
T tmp = T();
ok = (is >> tmp) ? true : false;
std::string rest;
is >> std::skipws >> rest;
if (!rest.empty()) ok = false;
return tmp;
}
template<>
bool stringTo<bool>(const std::string& s, bool& ok);
template <typename IntType>
IntType gcd(IntType n, IntType m)
{
// Avoid repeated construction
IntType zero(0);
// This is abs() - given the existence of broken compilers with Koenig
// lookup issues and other problems, I code this explicitly. (Remember,
// IntType may be a user-defined type).
#ifdef _MSC_VER
#pragma warning( disable : 4146 )
#undef max
#undef min
#endif
if (n < zero) {
if (n == std::numeric_limits<IntType>::min()) {
n = std::numeric_limits<IntType>::max();
} else {
n = -n;
}
}
if (m < zero)
m = -m;
#ifdef _MSC_VER
#pragma warning( default : 4146 )
#endif
// As n and m are now positive, we can be sure that %= returns a
// positive value (the standard guarantees this for built-in types,
// and we require it of user-defined types).
for(;;) {
if(m == zero)
return n;
n %= m;
if(n == zero)
return m;
m %= n;
}
}
} // namespace Exiv2
#endif // #ifndef TYPES_HPP_
Exiv2::DataBuf
Utility class containing a character array. All it does is to take care of memory allocation and dele...
Definition: types.hpp:193
Exiv2::DataBuf::size_
long size_
The current size of the buffer.
Definition: types.hpp:260
Exiv2::DataBuf::pData_
byte * pData_
Pointer to the buffer, 0 if none has been allocated.
Definition: types.hpp:258
Exiv2::TypeInfo
Type information lookup functions. Implemented as a static class.
Definition: types.hpp:157
Exiv2
Provides classes and functions to encode and decode Exif and Iptc data. The libexiv2 API consists of ...
Definition: asfvideo.hpp:36
Exiv2::operator<<
EXIV2API std::ostream & operator<<(std::ostream &os, const DataSet &dataSet)
Output operator for dataSet.
Definition: datasets.cpp:709
Exiv2::getDouble
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:356
Exiv2::hexdump
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:513
Exiv2::f2Data
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:464
Exiv2::d2Data
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:478
Exiv2::byte
uint8_t byte
1 byte unsigned integer type.
Definition: types.hpp:94
Exiv2::parseLong
EXIV2API long parseLong(const std::string &s, bool &ok)
Return a long set to the value represented by s.
Definition: types.cpp:626
Exiv2::getFloat
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:342
Exiv2::parseFloat
EXIV2API float parseFloat(const std::string &s, bool &ok)
Return a float set to the value represented by s.
Definition: types.cpp:650
Exiv2::isHex
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:538
Exiv2::floatToRationalCast
EXIV2API Rational floatToRationalCast(float f)
Very simple conversion of a float to a Rational.
Definition: types.cpp:689
Exiv2::l2Data
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:440
Exiv2::gcd
IntType gcd(IntType n, IntType m)
Return the greatest common denominator of n and m. (Implementation from Boost rational....
Definition: types.hpp:559
Exiv2::exvGettext
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:571
Exiv2::getShort
EXIV2API int16_t getShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte signed short value from the data buffer.
Definition: types.cpp:313
Exiv2::getULong
EXIV2API uint32_t getULong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte unsigned long value from the data buffer.
Definition: types.cpp:278
Exiv2::ul2Data
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:403
Exiv2::TypeId
TypeId
Exiv2 value type identifiers.
Definition: types.hpp:119
Exiv2::unsignedShort
@ unsignedShort
Exif SHORT type, 16-bit (2-byte) unsigned integer.
Definition: types.hpp:122
Exiv2::date
@ date
IPTC date type.
Definition: types.hpp:137
Exiv2::signedRational
@ signedRational
Exif SRATIONAL type, two SLONGs: numerator and denumerator of a fraction.
Definition: types.hpp:129
Exiv2::tiffIfd8
@ tiffIfd8
TIFF IFD type, 64-bit (8-byte) unsigned integer.
Definition: types.hpp:135
Exiv2::lastTypeId
@ lastTypeId
Last type id.
Definition: types.hpp:147
Exiv2::unsignedLongLong
@ unsignedLongLong
Exif LONG LONG type, 64-bit (8-byte) unsigned integer.
Definition: types.hpp:133
Exiv2::unsignedLong
@ unsignedLong
Exif LONG type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:123
Exiv2::signedShort
@ signedShort
Exif SSHORT type, a 16-bit (2-byte) signed (twos-complement) integer.
Definition: types.hpp:127
Exiv2::signedLongLong
@ signedLongLong
Exif LONG LONG type, 64-bit (8-byte) signed integer.
Definition: types.hpp:134
Exiv2::signedLong
@ signedLong
Exif SLONG type, a 32-bit (4-byte) signed (twos-complement) integer.
Definition: types.hpp:128
Exiv2::langAlt
@ langAlt
XMP language alternative type.
Definition: types.hpp:145
Exiv2::xmpAlt
@ xmpAlt
XMP alternative type.
Definition: types.hpp:142
Exiv2::unsignedByte
@ unsignedByte
Exif BYTE type, 8-bit unsigned integer.
Definition: types.hpp:120
Exiv2::signedByte
@ signedByte
Exif SBYTE type, an 8-bit signed (twos-complement) integer.
Definition: types.hpp:125
Exiv2::asciiString
@ asciiString
Exif ASCII type, 8-bit byte.
Definition: types.hpp:121
Exiv2::xmpText
@ xmpText
XMP text type.
Definition: types.hpp:141
Exiv2::time
@ time
IPTC time type.
Definition: types.hpp:138
Exiv2::xmpSeq
@ xmpSeq
XMP sequence type.
Definition: types.hpp:144
Exiv2::comment
@ comment
Exiv2 type for the Exif user comment.
Definition: types.hpp:139
Exiv2::tiffDouble
@ tiffDouble
TIFF DOUBLE type, double precision (8-byte) IEEE format.
Definition: types.hpp:131
Exiv2::undefined
@ undefined
Exif UNDEFINED type, an 8-bit byte that may contain anything.
Definition: types.hpp:126
Exiv2::xmpBag
@ xmpBag
XMP bag type.
Definition: types.hpp:143
Exiv2::tiffFloat
@ tiffFloat
TIFF FLOAT type, single precision (4-byte) IEEE format.
Definition: types.hpp:130
Exiv2::unsignedRational
@ unsignedRational
Exif RATIONAL type, two LONGs: numerator and denumerator of a fraction.
Definition: types.hpp:124
Exiv2::tiffIfd
@ tiffIfd
TIFF IFD type, 32-bit (4-byte) unsigned integer.
Definition: types.hpp:132
Exiv2::invalidTypeId
@ invalidTypeId
Invalid type id.
Definition: types.hpp:146
Exiv2::directory
@ directory
Exiv2 type for a CIFF directory.
Definition: types.hpp:140
Exiv2::stringTo< bool >
bool stringTo< bool >(const std::string &s, bool &ok)
Specialization of stringTo(const std::string& s, bool& ok) for bool.
Definition: types.cpp:607
Exiv2::getUShort
EXIV2API uint16_t getUShort(const byte *buf, ByteOrder byteOrder)
Read a 2 byte unsigned short value from the data buffer.
Definition: types.cpp:273
Exiv2::parseRational
EXIV2API Rational parseRational(const std::string &s, bool &ok)
Return a Rational set to the value represented by s.
Definition: types.cpp:671
Exiv2::Blob
std::vector< byte > Blob
Container for binary data.
Definition: types.hpp:151
Exiv2::sizer
char(& sizer(T(&)[N]))[N]
Template used in the COUNTOF macro to determine the size of an array.
Exiv2::makeSlice
Slice< T > makeSlice(T &cont, size_t begin, size_t end)
Return a new slice with the given bounds.
Definition: slice.hpp:665
Exiv2::ByteOrder
ByteOrder
Type to express the byte order (little or big endian)
Definition: types.hpp:102
Exiv2::stringTo
T stringTo(const std::string &s, bool &ok)
Utility function to convert a string to a value of type T.
Definition: types.hpp:529
Exiv2::Rational
std::pair< int32_t, int32_t > Rational
8 byte signed rational type.
Definition: types.hpp:99
Exiv2::getULongLong
EXIV2API uint64_t getULongLong(const byte *buf, ByteOrder byteOrder)
Read a 8 byte unsigned long value from the data buffer.
Definition: types.cpp:290
Exiv2::WriteMethod
WriteMethod
Type to indicate write method used by TIFF parsers.
Definition: types.hpp:105
Exiv2::getRational
EXIV2API Rational getRational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte signed rational value from the data buffer.
Definition: types.cpp:335
Exiv2::r2Data
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:457
Exiv2::MetadataId
MetadataId
An identifier for each type of metadata.
Definition: types.hpp:108
Exiv2::ur2Data
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:420
Exiv2::operator>>
EXIV2API std::istream & operator>>(std::istream &is, Rational &r)
Input operator for our fake rational.
Definition: types.cpp:223
Exiv2::getLong
EXIV2API int32_t getLong(const byte *buf, ByteOrder byteOrder)
Read a 4 byte signed long value from the data buffer.
Definition: types.cpp:323
Exiv2::s2Data
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:427
Exiv2::find
const T * find(T(&src)[N], const K &key)
Find an element that matches key in the array src.
Definition: types.hpp:497
Exiv2::toString
std::string toString(const T &arg)
Utility function to convert the argument of any type to a string.
Definition: types.hpp:510
Exiv2::URational
std::pair< uint32_t, uint32_t > URational
8 byte unsigned rational type.
Definition: types.hpp:97
Exiv2::getURational
EXIV2API URational getURational(const byte *buf, ByteOrder byteOrder)
Read an 8 byte unsigned rational value from the data buffer.
Definition: types.cpp:306
Exiv2::AccessMode
AccessMode
An identifier for each mode of metadata support.
Definition: types.hpp:111
Exiv2::exifTime
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:551
Exiv2::us2Data
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:390
Exiv2::DataBufRef
Auxiliary type to enable copies and assignments, similar to std::auto_ptr_ref. See http://www....
Definition: types.hpp:180
Exiv2::DataBufRef::DataBufRef
DataBufRef(std::pair< byte *, long > rhs)
Constructor.
Definition: types.hpp:182
Exiv2::DataBufRef::p
std::pair< byte *, long > p
Pointer to a byte array and its size.
Definition: types.hpp:184
Exiv2::Internal::MutableSliceBase::at
value_type & at(size_t index)
Definition: slice.hpp:261
Exiv2::Slice
Slice (= view) for STL containers.
Definition: slice.hpp:522