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