1 /****************************************************************************
2 ** libebml : parse EBML files, see http://embl.sourceforge.net/
4 ** <file/class description>
6 ** Copyright (C) 2002-2004 Steve Lhomme. All rights reserved.
8 ** This file is part of libebml.
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** Lesser General Public License for more details.
20 ** You should have received a copy of the GNU Lesser General Public
21 ** License along with this library; if not, write to the Free Software
22 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
26 ** Contact license@matroska.org if any conditions of this licensing are
29 **********************************************************************/
33 \version \$Id: EbmlEndian.h 1298 2008-02-21 22:14:18Z mosu $
34 \author Ingo Ralf Blum <ingoralfblum @ users.sf.net>
35 \author Lasse Kärkkäinen <tronic @ users.sf.net>
36 \author Steve Lhomme <robux4 @ users.sf.net>
38 #ifndef LIBEBML_ENDIAN_H
39 #define LIBEBML_ENDIAN_H
44 #include "EbmlConfig.h" // contains _ENDIANESS_
46 START_LIBEBML_NAMESPACE
49 big_endian
, ///< PowerPC, Alpha, 68000
50 little_endian
///< Intel x86 platforms
55 \brief general class to handle endian-specific buffers
56 \note don't forget to define/undefine _ENDIANESS_ to BIG_ENDIAN depending on your machine
58 template<class TYPE
, endianess ENDIAN
> class Endian
63 Endian(const TYPE value
)
65 memcpy(&platform_value
, &value
, sizeof(TYPE
));
69 inline Endian
& Eval(const binary
*endian_buffer
)
71 //endian_value = *(TYPE *)(endian_buffer);
72 memcpy(&endian_value
, endian_buffer
, sizeof(TYPE
)); // Some (all?) RISC processors do not allow reading objects bigger than 1 byte from non-aligned addresses, and endian_buffer may point to a non-aligned address.
77 inline void Fill(binary
*endian_buffer
) const
79 //*(TYPE*)endian_buffer = endian_value;
80 memcpy(endian_buffer
, &endian_value
, sizeof(TYPE
)); // See above.
83 inline operator const TYPE
&() const { return platform_value
; }
84 // inline TYPE endian() const { return endian_value; }
85 inline const TYPE
&endian() const { return endian_value
; }
86 inline size_t size() const { return sizeof(TYPE
); }
87 inline bool operator!=(const binary
*buffer
) const {return *((TYPE
*)buffer
) == platform_value
;}
93 inline void process_endian()
95 endian_value
= platform_value
;
96 #ifdef WORDS_BIGENDIAN
97 if (ENDIAN
== little_endian
)
98 std::reverse(reinterpret_cast<uint8
*>(&endian_value
),reinterpret_cast<uint8
*>(&endian_value
+1));
100 if (ENDIAN
== big_endian
)
101 std::reverse(reinterpret_cast<uint8
*>(&endian_value
),reinterpret_cast<uint8
*>(&endian_value
+1));
102 #endif // _ENDIANESS_
105 inline void process_platform()
107 platform_value
= endian_value
;
108 #ifdef WORDS_BIGENDIAN
109 if (ENDIAN
== little_endian
)
110 std::reverse(reinterpret_cast<uint8
*>(&platform_value
),reinterpret_cast<uint8
*>(&platform_value
+1));
112 if (ENDIAN
== big_endian
)
113 std::reverse(reinterpret_cast<uint8
*>(&platform_value
),reinterpret_cast<uint8
*>(&platform_value
+1));
114 #endif // _ENDIANESS_
118 END_LIBEBML_NAMESPACE
120 #endif // LIBEBML_ENDIAN_H