read_write_int.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @file read_write_int.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_READ_WRITE_INT_H
  30. #define BADVPN_READ_WRITE_INT_H
  31. #include <stdint.h>
  32. static uint8_t badvpn_read_le8 (const char *c_ptr);
  33. static uint16_t badvpn_read_le16 (const char *c_ptr);
  34. static uint32_t badvpn_read_le32 (const char *c_ptr);
  35. static uint64_t badvpn_read_le64 (const char *c_ptr);
  36. static uint8_t badvpn_read_be8 (const char *c_ptr);
  37. static uint16_t badvpn_read_be16 (const char *c_ptr);
  38. static uint32_t badvpn_read_be32 (const char *c_ptr);
  39. static uint64_t badvpn_read_be64 (const char *c_ptr);
  40. static void badvpn_write_le8 (uint8_t x, char *c_ptr);
  41. static void badvpn_write_le16 (uint16_t x, char *c_ptr);
  42. static void badvpn_write_le32 (uint32_t x, char *c_ptr);
  43. static void badvpn_write_le64 (uint64_t x, char *c_ptr);
  44. static void badvpn_write_be8 (uint8_t x, char *c_ptr);
  45. static void badvpn_write_be16 (uint16_t x, char *c_ptr);
  46. static void badvpn_write_be32 (uint32_t x, char *c_ptr);
  47. static void badvpn_write_be64 (uint64_t x, char *c_ptr);
  48. static uint8_t badvpn_read_le8 (const char *c_ptr)
  49. {
  50. const uint8_t *ptr = (void *)c_ptr;
  51. return ((uint8_t)ptr[0] << 0);
  52. }
  53. static uint16_t badvpn_read_le16 (const char *c_ptr)
  54. {
  55. const uint8_t *ptr = (void *)c_ptr;
  56. return ((uint16_t)ptr[1] << 8) | ((uint16_t)ptr[0] << 0);
  57. }
  58. static uint32_t badvpn_read_le32 (const char *c_ptr)
  59. {
  60. const uint8_t *ptr = (void *)c_ptr;
  61. return ((uint32_t)ptr[3] << 24) | ((uint32_t)ptr[2] << 16) |
  62. ((uint32_t)ptr[1] << 8) | ((uint32_t)ptr[0] << 0);
  63. }
  64. static uint64_t badvpn_read_le64 (const char *c_ptr)
  65. {
  66. const uint8_t *ptr = (void *)c_ptr;
  67. return ((uint64_t)ptr[7] << 56) | ((uint64_t)ptr[6] << 48) |
  68. ((uint64_t)ptr[5] << 40) | ((uint64_t)ptr[4] << 32) |
  69. ((uint64_t)ptr[3] << 24) | ((uint64_t)ptr[2] << 16) |
  70. ((uint64_t)ptr[1] << 8) | ((uint64_t)ptr[0] << 0);
  71. }
  72. static uint8_t badvpn_read_be8 (const char *c_ptr)
  73. {
  74. const uint8_t *ptr = (void *)c_ptr;
  75. return ((uint8_t)ptr[0] << 0);
  76. }
  77. static uint16_t badvpn_read_be16 (const char *c_ptr)
  78. {
  79. const uint8_t *ptr = (void *)c_ptr;
  80. return ((uint16_t)ptr[0] << 8) | ((uint16_t)ptr[1] << 0);
  81. }
  82. static uint32_t badvpn_read_be32 (const char *c_ptr)
  83. {
  84. const uint8_t *ptr = (void *)c_ptr;
  85. return ((uint32_t)ptr[0] << 24) | ((uint32_t)ptr[1] << 16) |
  86. ((uint32_t)ptr[2] << 8) | ((uint32_t)ptr[3] << 0);
  87. }
  88. static uint64_t badvpn_read_be64 (const char *c_ptr)
  89. {
  90. const uint8_t *ptr = (void *)c_ptr;
  91. return ((uint64_t)ptr[0] << 56) | ((uint64_t)ptr[1] << 48) |
  92. ((uint64_t)ptr[2] << 40) | ((uint64_t)ptr[3] << 32) |
  93. ((uint64_t)ptr[4] << 24) | ((uint64_t)ptr[5] << 16) |
  94. ((uint64_t)ptr[6] << 8) | ((uint64_t)ptr[7] << 0);
  95. }
  96. static void badvpn_write_le8 (uint8_t x, char *c_ptr)
  97. {
  98. uint8_t *ptr = (void *)c_ptr;
  99. ptr[0] = x >> 0;
  100. }
  101. static void badvpn_write_le16 (uint16_t x, char *c_ptr)
  102. {
  103. uint8_t *ptr = (void *)c_ptr;
  104. ptr[1] = x >> 8;
  105. ptr[0] = x >> 0;
  106. }
  107. static void badvpn_write_le32 (uint32_t x, char *c_ptr)
  108. {
  109. uint8_t *ptr = (void *)c_ptr;
  110. ptr[3] = x >> 24;
  111. ptr[2] = x >> 16;
  112. ptr[1] = x >> 8;
  113. ptr[0] = x >> 0;
  114. }
  115. static void badvpn_write_le64 (uint64_t x, char *c_ptr)
  116. {
  117. uint8_t *ptr = (void *)c_ptr;
  118. ptr[7] = x >> 56;
  119. ptr[6] = x >> 48;
  120. ptr[5] = x >> 40;
  121. ptr[4] = x >> 32;
  122. ptr[3] = x >> 24;
  123. ptr[2] = x >> 16;
  124. ptr[1] = x >> 8;
  125. ptr[0] = x >> 0;
  126. }
  127. static void badvpn_write_be8 (uint8_t x, char *c_ptr)
  128. {
  129. uint8_t *ptr = (void *)c_ptr;
  130. ptr[0] = x >> 0;
  131. }
  132. static void badvpn_write_be16 (uint16_t x, char *c_ptr)
  133. {
  134. uint8_t *ptr = (void *)c_ptr;
  135. ptr[0] = x >> 8;
  136. ptr[1] = x >> 0;
  137. }
  138. static void badvpn_write_be32 (uint32_t x, char *c_ptr)
  139. {
  140. uint8_t *ptr = (void *)c_ptr;
  141. ptr[0] = x >> 24;
  142. ptr[1] = x >> 16;
  143. ptr[2] = x >> 8;
  144. ptr[3] = x >> 0;
  145. }
  146. static void badvpn_write_be64 (uint64_t x, char *c_ptr)
  147. {
  148. uint8_t *ptr = (void *)c_ptr;
  149. ptr[0] = x >> 56;
  150. ptr[1] = x >> 48;
  151. ptr[2] = x >> 40;
  152. ptr[3] = x >> 32;
  153. ptr[4] = x >> 24;
  154. ptr[5] = x >> 16;
  155. ptr[6] = x >> 8;
  156. ptr[7] = x >> 0;
  157. }
  158. #endif