1/****************************************************************************
2** libebml : parse EBML files, see http://embl.sourceforge.net/
3**
4** <file/class description>
5**
6** Copyright (C) 2002-2004 Steve Lhomme.  All rights reserved.
7**
8** This file is part of libebml.
9**
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.
14**
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.
19**
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
23**
24** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
25**
26** Contact license@matroska.org if any conditions of this licensing are
27** not clear to you.
28**
29**********************************************************************/
30
31/*!
32    \file
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>
37*/
38#ifndef LIBEBML_ENDIAN_H
39#define LIBEBML_ENDIAN_H
40
41#include <algorithm>
42#include <cstring>
43
44#include "EbmlConfig.h" // contains _ENDIANESS_
45
46START_LIBEBML_NAMESPACE
47
48enum endianess {
49    big_endian,   ///< PowerPC, Alpha, 68000
50    little_endian ///< Intel x86 platforms
51};
52
53/*!
54    \class Endian
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
57*/
58template<class TYPE, endianess ENDIAN> class Endian
59{
60    public:
61	Endian() {}
62
63	Endian(const TYPE value)
64	{
65		memcpy(&platform_value, &value, sizeof(TYPE));
66		process_endian();
67	}
68
69	inline Endian & Eval(const binary *endian_buffer)
70	{
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.
73	    process_platform();
74	    return *this;
75	}
76
77	inline void Fill(binary *endian_buffer) const
78	{
79	    //*(TYPE*)endian_buffer = endian_value;
80	    memcpy(endian_buffer, &endian_value, sizeof(TYPE)); // See above.
81	}
82
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;}
88
89    protected:
90	TYPE platform_value;
91	TYPE endian_value;
92
93	inline void process_endian()
94	{
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));
99#else  // _ENDIANESS_
100	    if (ENDIAN == big_endian)
101		std::reverse(reinterpret_cast<uint8*>(&endian_value),reinterpret_cast<uint8*>(&endian_value+1));
102#endif // _ENDIANESS_
103	}
104
105	inline void process_platform()
106	{
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));
111#else  // _ENDIANESS_
112	    if (ENDIAN == big_endian)
113		std::reverse(reinterpret_cast<uint8*>(&platform_value),reinterpret_cast<uint8*>(&platform_value+1));
114#endif // _ENDIANESS_
115	}
116};
117
118END_LIBEBML_NAMESPACE
119
120#endif // LIBEBML_ENDIAN_H
121