/src/vlc/contrib/x86_64-unknown-linux-gnu/include/ebml/EbmlEndian.h
Line | Count | Source |
1 | | /**************************************************************************** |
2 | | ** libebml : parse EBML files, see http://embl.sourceforge.net/ |
3 | | ** |
4 | | ** <file/class description> |
5 | | ** |
6 | | ** Copyright (C) 2002-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | | ** |
24 | | ** See http://www.gnu.org/licenses/lgpl-2.1.html 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 | | |
46 | | namespace libebml { |
47 | | |
48 | | enum 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 | | */ |
58 | | template<class TYPE, endianess ENDIAN> class Endian |
59 | | { |
60 | | public: |
61 | | Endian() = default; |
62 | | |
63 | | Endian(const TYPE value) |
64 | 0 | { |
65 | 0 | memcpy(&platform_value, &value, sizeof(TYPE)); |
66 | 0 | process_endian(); |
67 | 0 | } |
68 | | |
69 | | inline Endian & Eval(const binary *endian_buffer) |
70 | 7.01k | { |
71 | | //endian_value = *(TYPE *)(endian_buffer); |
72 | 7.01k | 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 | 7.01k | process_platform(); |
74 | 7.01k | return *this; |
75 | 7.01k | } |
76 | | |
77 | | inline void Fill(binary *endian_buffer) const |
78 | 0 | { |
79 | | //*(TYPE*)endian_buffer = endian_value; |
80 | 0 | memcpy(endian_buffer, &endian_value, sizeof(TYPE)); // See above. |
81 | 0 | } |
82 | | |
83 | 7.01k | 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 *(reinterpret_cast<TYPE*>(buffer)) == platform_value;} |
88 | | |
89 | | #if defined(EBML_STRICT_API) |
90 | | private: |
91 | | #else |
92 | | protected: |
93 | | #endif |
94 | | TYPE platform_value; |
95 | | TYPE endian_value; |
96 | | |
97 | | inline void process_endian() |
98 | 0 | { |
99 | 0 | endian_value = platform_value; |
100 | | #ifdef WORDS_BIGENDIAN |
101 | | if (ENDIAN == little_endian) |
102 | | #else // _ENDIANESS_ |
103 | 0 | if (ENDIAN == big_endian) |
104 | 0 | #endif // _ENDIANESS_ |
105 | 0 | std::reverse(reinterpret_cast<uint8*>(&endian_value),reinterpret_cast<uint8*>(&endian_value+1)); |
106 | 0 | } |
107 | | |
108 | | inline void process_platform() |
109 | 7.01k | { |
110 | 7.01k | platform_value = endian_value; |
111 | | #ifdef WORDS_BIGENDIAN |
112 | | if (ENDIAN == little_endian) |
113 | | #else // _ENDIANESS_ |
114 | 7.01k | if (ENDIAN == big_endian) |
115 | 7.01k | #endif // _ENDIANESS_ |
116 | 7.01k | std::reverse(reinterpret_cast<uint8*>(&platform_value),reinterpret_cast<uint8*>(&platform_value+1)); |
117 | 7.01k | } |
118 | | }; |
119 | | |
120 | | } // namespace libebml |
121 | | |
122 | | #endif // LIBEBML_ENDIAN_H |