byte_io.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #ifndef MODULES_RTP_RTCP_SOURCE_BYTE_IO_H_
  2. #define MODULES_RTP_RTCP_SOURCE_BYTE_IO_H_
  3. // This file contains classes for reading and writing integer types from/to
  4. // byte array representations. Signed/unsigned, partial (whole byte) sizes,
  5. // and big/little endian byte order is all supported.
  6. //
  7. // Usage examples:
  8. //
  9. // uint8_t* buffer = ...;
  10. //
  11. // // Read an unsigned 4 byte integer in big endian format
  12. // uint32_t val = ByteReader<uint32_t>::ReadBigEndian(buffer);
  13. //
  14. // // Read a signed 24-bit (3 byte) integer in little endian format
  15. // int32_t val = ByteReader<int32_t, 3>::ReadLittle(buffer);
  16. //
  17. // // Write an unsigned 8 byte integer in little endian format
  18. // ByteWriter<uint64_t>::WriteLittleEndian(buffer, val);
  19. //
  20. // Write an unsigned 40-bit (5 byte) integer in big endian format
  21. // ByteWriter<uint64_t, 5>::WriteBigEndian(buffer, val);
  22. //
  23. // These classes are implemented as recursive templetizations, inteded to make
  24. // it easy for the compiler to completely inline the reading/writing.
  25. #include <stdint.h>
  26. #include <limits>
  27. // According to ISO C standard ISO/IEC 9899, section 6.2.6.2 (2), the three
  28. // representations of signed integers allowed are two's complement, one's
  29. // complement and sign/magnitude. We can detect which is used by looking at
  30. // the two last bits of -1, which will be 11 in two's complement, 10 in one's
  31. // complement and 01 in sign/magnitude.
  32. // TODO(sprang): In the unlikely event that we actually need to support a
  33. // platform that doesn't use two's complement, implement conversion to/from
  34. // wire format.
  35. // Assume the if any one signed integer type is two's complement, then all
  36. // other will be too.
  37. static_assert(
  38. (-1 & 0x03) == 0x03,
  39. "Only two's complement representation of signed integers supported.");
  40. // Plain const char* won't work for static_assert, use #define instead.
  41. #define kSizeErrorMsg "Byte size must be less than or equal to data type size."
  42. // Utility class for getting the unsigned equivalent of a signed type.
  43. template <typename T>
  44. struct UnsignedOf;
  45. // Class for reading integers from a sequence of bytes.
  46. // T = type of integer, B = bytes to read, is_signed = true if signed integer.
  47. // If is_signed is true and B < sizeof(T), sign extension might be needed.
  48. template <typename T,
  49. unsigned int B = sizeof(T),
  50. bool is_signed = std::numeric_limits<T>::is_signed>
  51. class ByteReader;
  52. // Specialization of ByteReader for unsigned types.
  53. template <typename T, unsigned int B>
  54. class ByteReader<T, B, false> {
  55. public:
  56. static T ReadBigEndian(const uint8_t* data) {
  57. static_assert(B <= sizeof(T), kSizeErrorMsg);
  58. return InternalReadBigEndian(data);
  59. }
  60. static T ReadLittleEndian(const uint8_t* data) {
  61. static_assert(B <= sizeof(T), kSizeErrorMsg);
  62. return InternalReadLittleEndian(data);
  63. }
  64. private:
  65. static T InternalReadBigEndian(const uint8_t* data) {
  66. T val(0);
  67. for (unsigned int i = 0; i < B; ++i)
  68. val |= static_cast<T>(data[i]) << ((B - 1 - i) * 8);
  69. return val;
  70. }
  71. static T InternalReadLittleEndian(const uint8_t* data) {
  72. T val(0);
  73. for (unsigned int i = 0; i < B; ++i)
  74. val |= static_cast<T>(data[i]) << (i * 8);
  75. return val;
  76. }
  77. };
  78. // Specialization of ByteReader for signed types.
  79. template <typename T, unsigned int B>
  80. class ByteReader<T, B, true> {
  81. public:
  82. typedef typename UnsignedOf<T>::Type U;
  83. static T ReadBigEndian(const uint8_t* data) {
  84. U unsigned_val = ByteReader<T, B, false>::ReadBigEndian(data);
  85. if (B < sizeof(T))
  86. unsigned_val = SignExtend(unsigned_val);
  87. return ReinterpretAsSigned(unsigned_val);
  88. }
  89. static T ReadLittleEndian(const uint8_t* data) {
  90. U unsigned_val = ByteReader<T, B, false>::ReadLittleEndian(data);
  91. if (B < sizeof(T))
  92. unsigned_val = SignExtend(unsigned_val);
  93. return ReinterpretAsSigned(unsigned_val);
  94. }
  95. private:
  96. // As a hack to avoid implementation-specific or undefined behavior when
  97. // bit-shifting or casting signed integers, read as a signed equivalent
  98. // instead and convert to signed. This is safe since we have asserted that
  99. // two's complement for is used.
  100. static T ReinterpretAsSigned(U unsigned_val) {
  101. // An unsigned value with only the highest order bit set (ex 0x80).
  102. const U kUnsignedHighestBitMask = static_cast<U>(1)
  103. << ((sizeof(U) * 8) - 1);
  104. // A signed value with only the highest bit set. Since this is two's
  105. // complement form, we can use the min value from std::numeric_limits.
  106. const T kSignedHighestBitMask = std::numeric_limits<T>::min();
  107. T val;
  108. if ((unsigned_val & kUnsignedHighestBitMask) != 0) {
  109. // Casting is only safe when unsigned value can be represented in the
  110. // signed target type, so mask out highest bit and mask it back manually.
  111. val = static_cast<T>(unsigned_val & ~kUnsignedHighestBitMask);
  112. val |= kSignedHighestBitMask;
  113. } else {
  114. val = static_cast<T>(unsigned_val);
  115. }
  116. return val;
  117. }
  118. // If number of bytes is less than native data type (eg 24 bit, in int32_t),
  119. // and the most significant bit of the actual data is set, we must sign
  120. // extend the remaining byte(s) with ones so that the correct negative
  121. // number is retained.
  122. // Ex: 0x810A0B -> 0xFF810A0B, but 0x710A0B -> 0x00710A0B
  123. static U SignExtend(const U val) {
  124. const uint8_t kMsb = static_cast<uint8_t>(val >> ((B - 1) * 8));
  125. if ((kMsb & 0x80) != 0) {
  126. // Create a mask where all bits used by the B bytes are set to one,
  127. // for instance 0x00FFFFFF for B = 3. Bit-wise invert that mask (to
  128. // (0xFF000000 in the example above) and add it to the input value.
  129. // The "B % sizeof(T)" is a workaround to undefined values warnings for
  130. // B == sizeof(T), in which case this code won't be called anyway.
  131. const U kUsedBitsMask = (1 << ((B % sizeof(T)) * 8)) - 1;
  132. return ~kUsedBitsMask | val;
  133. }
  134. return val;
  135. }
  136. };
  137. // Class for writing integers to a sequence of bytes
  138. // T = type of integer, B = bytes to write
  139. template <typename T,
  140. unsigned int B = sizeof(T),
  141. bool is_signed = std::numeric_limits<T>::is_signed>
  142. class ByteWriter;
  143. // Specialization of ByteWriter for unsigned types.
  144. template <typename T, unsigned int B>
  145. class ByteWriter<T, B, false> {
  146. public:
  147. static void WriteBigEndian(uint8_t* data, T val) {
  148. static_assert(B <= sizeof(T), kSizeErrorMsg);
  149. for (unsigned int i = 0; i < B; ++i) {
  150. data[i] = val >> ((B - 1 - i) * 8);
  151. }
  152. }
  153. static void WriteLittleEndian(uint8_t* data, T val) {
  154. static_assert(B <= sizeof(T), kSizeErrorMsg);
  155. for (unsigned int i = 0; i < B; ++i) {
  156. data[i] = val >> (i * 8);
  157. }
  158. }
  159. };
  160. // Specialization of ByteWriter for signed types.
  161. template <typename T, unsigned int B>
  162. class ByteWriter<T, B, true> {
  163. public:
  164. typedef typename UnsignedOf<T>::Type U;
  165. static void WriteBigEndian(uint8_t* data, T val) {
  166. ByteWriter<U, B, false>::WriteBigEndian(data, ReinterpretAsUnsigned(val));
  167. }
  168. static void WriteLittleEndian(uint8_t* data, T val) {
  169. ByteWriter<U, B, false>::WriteLittleEndian(data,
  170. ReinterpretAsUnsigned(val));
  171. }
  172. private:
  173. static U ReinterpretAsUnsigned(T val) {
  174. // According to ISO C standard ISO/IEC 9899, section 6.3.1.3 (1, 2) a
  175. // conversion from signed to unsigned keeps the value if the new type can
  176. // represent it, and otherwise adds one more than the max value of T until
  177. // the value is in range. For two's complement, this fortunately means
  178. // that the bit-wise value will be intact. Thus, since we have asserted that
  179. // two's complement form is actually used, a simple cast is sufficient.
  180. return static_cast<U>(val);
  181. }
  182. };
  183. // ----- Below follows specializations of UnsignedOf utility class -----
  184. template <>
  185. struct UnsignedOf<int8_t> {
  186. typedef uint8_t Type;
  187. };
  188. template <>
  189. struct UnsignedOf<int16_t> {
  190. typedef uint16_t Type;
  191. };
  192. template <>
  193. struct UnsignedOf<int32_t> {
  194. typedef uint32_t Type;
  195. };
  196. template <>
  197. struct UnsignedOf<int64_t> {
  198. typedef uint64_t Type;
  199. };
  200. // ----- Below follows specializations for unsigned, B in { 1, 2, 4, 8 } -----
  201. // TODO(sprang): Check if these actually help or if generic cases will be
  202. // unrolled to and optimized to similar performance.
  203. // Specializations for single bytes
  204. template <typename T>
  205. class ByteReader<T, 1, false> {
  206. public:
  207. static T ReadBigEndian(const uint8_t* data) {
  208. static_assert(sizeof(T) == 1, kSizeErrorMsg);
  209. return data[0];
  210. }
  211. static T ReadLittleEndian(const uint8_t* data) {
  212. static_assert(sizeof(T) == 1, kSizeErrorMsg);
  213. return data[0];
  214. }
  215. };
  216. template <typename T>
  217. class ByteWriter<T, 1, false> {
  218. public:
  219. static void WriteBigEndian(uint8_t* data, T val) {
  220. static_assert(sizeof(T) == 1, kSizeErrorMsg);
  221. data[0] = val;
  222. }
  223. static void WriteLittleEndian(uint8_t* data, T val) {
  224. static_assert(sizeof(T) == 1, kSizeErrorMsg);
  225. data[0] = val;
  226. }
  227. };
  228. // Specializations for two byte words
  229. template <typename T>
  230. class ByteReader<T, 2, false> {
  231. public:
  232. static T ReadBigEndian(const uint8_t* data) {
  233. static_assert(sizeof(T) >= 2, kSizeErrorMsg);
  234. return (data[0] << 8) | data[1];
  235. }
  236. static T ReadLittleEndian(const uint8_t* data) {
  237. static_assert(sizeof(T) >= 2, kSizeErrorMsg);
  238. return data[0] | (data[1] << 8);
  239. }
  240. };
  241. template <typename T>
  242. class ByteWriter<T, 2, false> {
  243. public:
  244. static void WriteBigEndian(uint8_t* data, T val) {
  245. static_assert(sizeof(T) >= 2, kSizeErrorMsg);
  246. data[0] = val >> 8;
  247. data[1] = val;
  248. }
  249. static void WriteLittleEndian(uint8_t* data, T val) {
  250. static_assert(sizeof(T) >= 2, kSizeErrorMsg);
  251. data[0] = val;
  252. data[1] = val >> 8;
  253. }
  254. };
  255. // Specializations for four byte words.
  256. template <typename T>
  257. class ByteReader<T, 4, false> {
  258. public:
  259. static T ReadBigEndian(const uint8_t* data) {
  260. static_assert(sizeof(T) >= 4, kSizeErrorMsg);
  261. return (Get(data, 0) << 24) | (Get(data, 1) << 16) | (Get(data, 2) << 8) |
  262. Get(data, 3);
  263. }
  264. static T ReadLittleEndian(const uint8_t* data) {
  265. static_assert(sizeof(T) >= 4, kSizeErrorMsg);
  266. return Get(data, 0) | (Get(data, 1) << 8) | (Get(data, 2) << 16) |
  267. (Get(data, 3) << 24);
  268. }
  269. private:
  270. inline static T Get(const uint8_t* data, unsigned int index) {
  271. return static_cast<T>(data[index]);
  272. }
  273. };
  274. // Specializations for four byte words.
  275. template <typename T>
  276. class ByteWriter<T, 4, false> {
  277. public:
  278. static void WriteBigEndian(uint8_t* data, T val) {
  279. static_assert(sizeof(T) >= 4, kSizeErrorMsg);
  280. data[0] = val >> 24;
  281. data[1] = val >> 16;
  282. data[2] = val >> 8;
  283. data[3] = val;
  284. }
  285. static void WriteLittleEndian(uint8_t* data, T val) {
  286. static_assert(sizeof(T) >= 4, kSizeErrorMsg);
  287. data[0] = val;
  288. data[1] = val >> 8;
  289. data[2] = val >> 16;
  290. data[3] = val >> 24;
  291. }
  292. };
  293. // Specializations for eight byte words.
  294. template <typename T>
  295. class ByteReader<T, 8, false> {
  296. public:
  297. static T ReadBigEndian(const uint8_t* data) {
  298. static_assert(sizeof(T) >= 8, kSizeErrorMsg);
  299. return (Get(data, 0) << 56) | (Get(data, 1) << 48) | (Get(data, 2) << 40) |
  300. (Get(data, 3) << 32) | (Get(data, 4) << 24) | (Get(data, 5) << 16) |
  301. (Get(data, 6) << 8) | Get(data, 7);
  302. }
  303. static T ReadLittleEndian(const uint8_t* data) {
  304. static_assert(sizeof(T) >= 8, kSizeErrorMsg);
  305. return Get(data, 0) | (Get(data, 1) << 8) | (Get(data, 2) << 16) |
  306. (Get(data, 3) << 24) | (Get(data, 4) << 32) | (Get(data, 5) << 40) |
  307. (Get(data, 6) << 48) | (Get(data, 7) << 56);
  308. }
  309. private:
  310. inline static T Get(const uint8_t* data, unsigned int index) {
  311. return static_cast<T>(data[index]);
  312. }
  313. };
  314. template <typename T>
  315. class ByteWriter<T, 8, false> {
  316. public:
  317. static void WriteBigEndian(uint8_t* data, T val) {
  318. static_assert(sizeof(T) >= 8, kSizeErrorMsg);
  319. data[0] = val >> 56;
  320. data[1] = val >> 48;
  321. data[2] = val >> 40;
  322. data[3] = val >> 32;
  323. data[4] = val >> 24;
  324. data[5] = val >> 16;
  325. data[6] = val >> 8;
  326. data[7] = val;
  327. }
  328. static void WriteLittleEndian(uint8_t* data, T val) {
  329. static_assert(sizeof(T) >= 8, kSizeErrorMsg);
  330. data[0] = val;
  331. data[1] = val >> 8;
  332. data[2] = val >> 16;
  333. data[3] = val >> 24;
  334. data[4] = val >> 32;
  335. data[5] = val >> 40;
  336. data[6] = val >> 48;
  337. data[7] = val >> 56;
  338. }
  339. };
  340. #endif // MODULES_RTP_RTCP_SOURCE_BYTE_IO_H_