00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00038 #ifndef LIBEBML_ENDIAN_H
00039 #define LIBEBML_ENDIAN_H
00040
00041 #include <algorithm>
00042 #include <cstring>
00043
00044 #include "EbmlConfig.h"
00045
00046 START_LIBEBML_NAMESPACE
00047
00048 enum endianess {
00049 big_endian,
00050 little_endian
00051 };
00052
00058 template<class TYPE, endianess ENDIAN> class Endian
00059 {
00060 public:
00061 Endian() {}
00062
00063 Endian(const TYPE value)
00064 {
00065 memcpy(&platform_value, &value, sizeof(TYPE));
00066 process_endian();
00067 }
00068
00069 inline Endian & Eval(const binary *endian_buffer)
00070 {
00071
00072 memcpy(&endian_value, endian_buffer, sizeof(TYPE));
00073 process_platform();
00074 return *this;
00075 }
00076
00077 inline void Fill(binary *endian_buffer) const
00078 {
00079
00080 memcpy(endian_buffer, &endian_value, sizeof(TYPE));
00081 }
00082
00083 inline operator const TYPE&() const { return platform_value; }
00084
00085 inline const TYPE &endian() const { return endian_value; }
00086 inline size_t size() const { return sizeof(TYPE); }
00087 inline bool operator!=(const binary *buffer) const {return *((TYPE*)buffer) == platform_value;}
00088
00089 #if defined(EBML_STRICT_API)
00090 private:
00091 #else
00092 protected:
00093 #endif
00094 TYPE platform_value;
00095 TYPE endian_value;
00096
00097 inline void process_endian()
00098 {
00099 endian_value = platform_value;
00100 #ifdef WORDS_BIGENDIAN
00101 if (ENDIAN == little_endian)
00102 #else // _ENDIANESS_
00103 if (ENDIAN == big_endian)
00104 #endif // _ENDIANESS_
00105 std::reverse(reinterpret_cast<uint8*>(&endian_value),reinterpret_cast<uint8*>(&endian_value+1));
00106 }
00107
00108 inline void process_platform()
00109 {
00110 platform_value = endian_value;
00111 #ifdef WORDS_BIGENDIAN
00112 if (ENDIAN == little_endian)
00113 #else // _ENDIANESS_
00114 if (ENDIAN == big_endian)
00115 #endif // _ENDIANESS_
00116 std::reverse(reinterpret_cast<uint8*>(&platform_value),reinterpret_cast<uint8*>(&platform_value+1));
00117 }
00118 };
00119
00120 END_LIBEBML_NAMESPACE
00121
00122 #endif // LIBEBML_ENDIAN_H