00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TOOL_ENDIANNESS_HXX_
00023 # define TOOL_ENDIANNESS_HXX_
00024
00025 # include <cassert>
00026
00027 # include "endianness.hh"
00028
00029 namespace tool
00030 {
00031
00032 namespace endian
00033 {
00034
00035 inline
00036 std::ostream&
00037 operator << (std::ostream& o, const endianness& e)
00038 {
00039 switch (e)
00040 {
00041 case big:
00042 return o << "big endian";
00043
00044 case little:
00045 return o << "little endian";
00046
00047 case raw:
00048 return o << "same endianness as host";
00049
00050 case swap:
00051 return o << "swap bytes";
00052
00053 case guess:
00054 return o << "guess endianness according to data";
00055
00056 default:
00057 assert(! "Internal error: undefined endianness.");
00058 return o << "unkown endianness value";
00059 }
00060 }
00061
00062 inline
00063 bool
00064 need_swap(endianness t, bool guess_swap)
00065 {
00066 static const int i = 1;
00067 static const bool l = reinterpret_cast<const char*> (&i)[0];
00068
00069 switch (t)
00070 {
00071 case big:
00072 return l;
00073
00074 case little:
00075 return not l;
00076
00077 case raw:
00078 return false;
00079
00080 case swap:
00081 return true;
00082
00083 case guess:
00084 return guess_swap;
00085
00086 default:
00087 assert(! "Internal error: undefined endianness.");
00088 return false;
00089 }
00090 }
00091
00092 }
00093
00094
00095
00096
00097
00098
00099 inline
00100 uint16_t
00101 extract_unswapped_short_u(const void* p)
00102 {
00103 return *static_cast<const uint16_t*> (p);
00104 }
00105
00106 inline
00107 uint16_t
00108 extract_swapped_short_u(const uint16_t s)
00109 {
00110 return (s >> 8) | (s << 8);
00111 }
00112
00113 inline
00114 uint16_t
00115 extract_swapped_short_u(const void* p)
00116 {
00117 return extract_swapped_short_u(extract_unswapped_short_u(p));
00118 }
00119
00120 inline
00121 uint16_t
00122 extract_short_u(const void* p, bool swapped)
00123 {
00124 return swapped ? extract_swapped_short_u(p) : extract_unswapped_short_u(p);
00125 }
00126
00127 inline
00128 uint16_t
00129 extract_short_u(const uint16_t s, bool swapped)
00130 {
00131 return swapped ? extract_swapped_short_u(s) : s;
00132 }
00133
00134 inline
00135 uint16_t
00136 extract_big_endian_short_u(const void *p)
00137 {
00138 const uint8_t *const c = static_cast<const uint8_t*> (p);
00139
00140 return uint16_t (c[1]) | uint16_t (c[0]) << 8;
00141 }
00142
00143 inline
00144 uint16_t
00145 extract_big_endian_short_u(const uint16_t s)
00146 {
00147 return extract_big_endian_short_u(&s);
00148 }
00149
00150 inline
00151 uint16_t
00152 extract_little_endian_short_u(const void *p)
00153 {
00154 const uint8_t *const c = static_cast<const uint8_t*> (p);
00155
00156 return uint16_t (c[0]) | uint16_t (c[1]) << 8;
00157 }
00158
00159 inline
00160 uint16_t
00161 extract_little_endian_short_u(const uint16_t s)
00162 {
00163 return extract_little_endian_short_u(&s);
00164 }
00165
00166
00167
00168
00169
00170 inline
00171 uint32_t
00172 extract_unswapped_long_u(const void* p)
00173 {
00174 return *static_cast<const uint32_t*> (p);
00175 }
00176
00177 inline
00178 uint32_t
00179 extract_swapped_long_u(const uint32_t l)
00180 {
00181 return (l >> 24) | ((l & 0xFF0000) >> 8) | ((l & 0xFF00) << 8) | (l << 24);
00182 }
00183
00184 inline
00185 uint32_t
00186 extract_swapped_long_u(const void* p)
00187 {
00188 return extract_swapped_long_u(extract_unswapped_long_u(p));
00189 }
00190
00191 inline
00192 uint32_t
00193 extract_long_u(const void* p, bool swapped)
00194 {
00195 return swapped ? extract_swapped_long_u(p) : extract_unswapped_long_u(p);
00196 }
00197
00198 inline
00199 uint32_t
00200 extract_long_u(const uint32_t l, bool swapped)
00201 {
00202 return swapped ? extract_swapped_long_u(l) : l;
00203 }
00204
00205 inline
00206 uint32_t
00207 extract_big_endian_long_u(const void* p)
00208 {
00209 const uint8_t *const c = static_cast<const uint8_t*> (p);
00210
00211 return (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
00212 }
00213
00214 inline
00215 uint32_t
00216 extract_big_endian_long_u(const uint32_t l)
00217 {
00218 return extract_big_endian_long_u(&l);
00219 }
00220
00221 inline
00222 uint32_t
00223 extract_little_endian_long_u(const void* p)
00224 {
00225 const uint8_t *const c = static_cast<const uint8_t*> (p);
00226
00227 return (c[3] << 24) | (c[2] << 16) | (c[1] << 8) | c[0];
00228 }
00229
00230 inline
00231 uint32_t
00232 extract_little_endian_long_u(const uint32_t l)
00233 {
00234 return extract_little_endian_long_u(&l);
00235 }
00236
00237
00238
00239
00240
00241 inline
00242 int32_t
00243 extract_unswapped_long_s(const void* p)
00244 {
00245 return int32_t (extract_unswapped_long_u(p));
00246 }
00247
00248 inline
00249 int32_t
00250 extract_swapped_long_s(const int32_t l)
00251 {
00252 return int32_t (extract_swapped_long_u(l));
00253 }
00254
00255 inline
00256 int32_t
00257 extract_swapped_long_s(const void* p)
00258 {
00259 return int32_t (extract_swapped_long_u(p));
00260 }
00261
00262 inline
00263 int32_t
00264 extract_long_s(const void* p, bool swapped)
00265 {
00266 return int32_t (extract_long_u(p, swapped));
00267 }
00268
00269 inline
00270 int32_t
00271 extract_long_s(const int32_t l, bool swapped)
00272 {
00273 return int32_t (extract_long_u(l, swapped));
00274 }
00275
00276 inline
00277 int32_t
00278 extract_big_endian_long_s(const void* p)
00279 {
00280 return int32_t (extract_big_endian_long_u(p));
00281 }
00282
00283 inline
00284 int32_t
00285 extract_big_endian_long_s(const int32_t l)
00286 {
00287 return int32_t (extract_big_endian_long_u(l));
00288 }
00289
00290 inline
00291 int32_t
00292 extract_little_endian_long_s(const void* p)
00293 {
00294 return int32_t (extract_little_endian_long_u(p));
00295 }
00296
00297 inline
00298 int32_t
00299 extract_little_endian_long_s(const int32_t l)
00300 {
00301 return int32_t (extract_little_endian_long_u(l));
00302 }
00303
00304 }
00305
00306 #endif // ! TOOL_ENDIANNESS_HXX_