EAByteCrackers.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>
  2. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  3. <Title>EAByteCrackers</title>
  4. <link type="text/css" rel="stylesheet" href="UTFDoc.css">
  5. <meta name="author" content="Paul Pedriana">
  6. </head>
  7. <body bgcolor="#FFFFFF">
  8. <h1>EAByteCrackers</h1>
  9. <h2>Introduction</h2>
  10. <p>EAByteCrackers is a small utility header file that provides macros for breaking down integers into component bytes and for building up integers from component
  11. bytes. These are particularly useful because working with signed values creates opportunities for mistakes, and the macros present in EAByteCrackers remove such
  12. possibilities for error. You can run into this need for dealing with signed values when working with Windows &quot;winproc&quot; message packing, for example.</p>
  13. <h2>Conventions</h2>
  14. <p>Components within an integer are numbered 0-N with 0 referring to the least significant component. Thus, the 32 bit value 0x12345678 consists of byte 0 = 0x78,
  15. byte 1 = 0x56, byte 2 = 0x34, and byte 3 = 0x12. This is done like so because the index of the byte is thus equal to its numerical shift within the integer. It
  16. also means that byte 2 of a uint32_t refers to the same value as byte 2 of a uint64_t. For example:</p>
  17. <pre class="code-example">uint16_t u16 = UINT16_5_FROM_UINT64(0x887766554433221100); <font color="#0066FF">// u16 becomes 0x55</font></pre>
  18. <p>Similarly, we have this:</p>
  19. <pre class="code-example">uint32_t u32 = UINT32_FROM_UINT8(0x33, 0x22, 0x11, 0x00); <font color="#0066FF">// u32 becomes 0x33221100</font></pre>
  20. <p>In the macros, the terms b, w, d, and q are used.</p>
  21. <ul>
  22. <li>b means 8 bit byte</li>
  23. <li>w means 16 bit word</li>
  24. <li>d means 32 bit dword</li>
  25. <li>q means 64 bit quadword</li>
  26. </ul>
  27. <h2>Example usage </h2>
  28. <p>Basic usage:</p>
  29. <pre class="code-example">// Integer breakdown
  30. uint8_t u8 = UINT8_0_FROM_UINT16(0x1100); <font color="#0066FF">// u8 becomes 0x00</font>
  31. uint8_t u8 = UINT8_2_FROM_UINT64(0x7766554433221100); <font color="#0066FF">// u8 becomes 0x22</font>
  32. uint16_t u16 = UINT16_3_FROM_UINT64(0x7766554433221100); <font color="#0066FF">// u16 becomes 0x7766</font>
  33. // Integer build up
  34. uint16_t u16 = UINT16_FROM_UINT8(0x11, 0x00); <font color="#0066FF">// u16 becomes 0x1100</font>
  35. uint32_t u32 = UINT32_FROM_UINT8(0x33, 0x22, 0x11, 0x00); <font color="#0066FF">// u32 becomes 0x33221100</font></pre>
  36. <p></p>
  37. <p>How to safely convert a 32 bit value to two signed 16 bit values:</p>
  38. <pre class="code-example">int16_t n0 = (int16_t)UINT16_0_FROM_UINT32(0x1000ffff); <font color="#0066FF">// n0 becomes -1</font>
  39. int16_t n1 = (int16_t)UINT16_1_FROM_UINT32(0x1000ffff); <font color="#0066FF">// n1 becomes 4096</font></pre>
  40. <p>How to build up a uint64_t from a uint32_t and two uint16_t values:</p>
  41. <pre class="code-example">uint64_t u64 = UINT64_FROM_UINT32(0x77665544, UINT32_FROM_UINT16(0x3322, 0x1100)); <font color="#0066FF">// u64 becomes 0x7766554433221100</font>
  42. </pre>
  43. <h2>Interface</h2>
  44. <pre class="code-example"><span class="code-example-comment">// uint8_t byte manipulators</span>
  45. #define UINT8_0_FROM_UINT16(w)
  46. #define UINT8_1_FROM_UINT16(w)
  47. #define UINT8_0_FROM_UINT32(d)
  48. #define UINT8_1_FROM_UINT32(d)
  49. #define UINT8_2_FROM_UINT32(d)
  50. #define UINT8_3_FROM_UINT32(d)
  51. #define UINT8_0_FROM_UINT64(q)
  52. #define UINT8_1_FROM_UINT64(q)
  53. #define UINT8_2_FROM_UINT64(q)
  54. #define UINT8_3_FROM_UINT64(q)
  55. #define UINT8_4_FROM_UINT64(q)
  56. #define UINT8_5_FROM_UINT64(q)
  57. #define UINT8_6_FROM_UINT64(q)
  58. #define UINT8_7_FROM_UINT64(q)
  59. <span class="code-example-comment">// uint16_t byte manipulators</span>
  60. #define UINT16_0_FROM_UINT32(d)
  61. #define UINT16_1_FROM_UINT32(d)
  62. #define UINT16_0_FROM_UINT64(q)
  63. #define UINT16_1_FROM_UINT64(q)
  64. #define UINT16_2_FROM_UINT64(q)
  65. #define UINT16_3_FROM_UINT64(q)
  66. #define UINT16_FROM_UINT8(b1, b0)
  67. <span class="code-example-comment">// uint32_t byte manipulators</span>
  68. #define UINT32_2_FROM_UINT64(q)
  69. #define UINT32_1_FROM_UINT64(q)
  70. #define UINT32_FROM_UINT8(b3, b2, b1, b0)
  71. #define UINT32_FROM_UINT16(w1, w0)
  72. <span class="code-example-comment">// uint64_t byte manipulators</span>
  73. #define UINT64_FROM_UINT8(b7, b6, b5, b4, b3, b2, b1, b0)
  74. #define UINT64_FROM_UINT16(w3, w2, w1, w0)
  75. #define UINT64_FROM_UINT32(d1, d0)
  76. </pre>
  77. <p></p>
  78. <hr>
  79. <p>&nbsp;</p>
  80. <p>&nbsp;</p>
  81. <p>&nbsp;</p>
  82. <p>&nbsp;</p>
  83. <p>&nbsp;</p>
  84. <p>&nbsp;</p>
  85. <p>&nbsp;</p>
  86. <p>&nbsp;</p>
  87. <p>&nbsp;</p>
  88. <p>&nbsp;</p>
  89. <p>&nbsp;</p>
  90. <p>&nbsp;</p>
  91. <p>&nbsp;</p>
  92. </body></html>