base16.h.xml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <chapter xml:id="base16.h">
  2. <title><tt>__vic/base16.h</tt></title>
  3. <p>Base16 encoder and decoder.</p>
  4. <chapter xml:id="base16">
  5. <title><tt>base16</tt></title>
  6. <p>A class used as a namespace. Contains only types, constants and static
  7. functions. No objects of this class are supposed to be created.</p>
  8. </chapter>
  9. <chapter xml:id="base16--bad_format">
  10. <title><tt>base16::bad_format</tt></title>
  11. <code-block lang="C++">
  12. struct base16::bad_format : public std::exception {};
  13. </code-block>
  14. <p>Abstract base exception class.</p>
  15. </chapter>
  16. <chapter xml:id="base16--bad_digit">
  17. <title><tt>base16::bad_digit</tt></title>
  18. <code-block lang="C++">
  19. struct base16::bad_digit : public base16::bad_format
  20. {
  21. const char *what() const noexcept;
  22. };
  23. </code-block>
  24. <p>Exception class thrown by <xref to="base16--decode"/> when the input sequence
  25. contains character that is not a valid HEX digit.</p>
  26. </chapter>
  27. <chapter xml:id="base16--bad_length">
  28. <title><tt>base16::bad_length</tt></title>
  29. <code-block lang="C++">
  30. struct base16::bad_length : public base16::bad_format
  31. {
  32. const char *what() const noexcept;
  33. };
  34. </code-block>
  35. <p>Exception class thrown by <xref to="base16--decode"/> when the input sequence
  36. length is odd.</p>
  37. </chapter>
  38. <chapter xml:id="base16--status">
  39. <title><tt>base16::status</tt></title>
  40. <code-block lang="C++">
  41. enum class base16::status
  42. {
  43. ok,
  44. invalid_length,
  45. invalid_digit
  46. };
  47. using base16::status_t = base16::status; // for C++98
  48. </code-block>
  49. <p>Input sequence parsing outcome status codes returned by
  50. <xref to="base16--try_decode"/>.</p>
  51. </chapter>
  52. <chapter xml:id="base16--encode_upper">
  53. <title><tt>base16::encode_upper()</tt></title>
  54. <code-block lang="C++"><![CDATA[
  55. // Bytes -> Text
  56. template<class ByteReader, class CharWriter>
  57. void base16::encode_upper(ByteReader reader, CharWriter writer);
  58. ]]></code-block>
  59. <p>Encodes bytes from <tt>reader</tt> and writes the resulting characters to
  60. <tt>writer</tt>. Upper case is used for hexadecimal digits.</p>
  61. <p><tt>ByteReader</tt> has to model <tt>Reader&lt;unsigned char></tt> concept.
  62. See <xref to="readers"/>.</p>
  63. <p><tt>CharWriter</tt> has to model <tt>Writer&lt;char></tt> concept.
  64. See <xref to="writers"/>.</p>
  65. <section><title>Example</title>
  66. <code-block lang="C++"><![CDATA[
  67. #include<__vic/readers/string.h>
  68. #include<__vic/writers/string.h>
  69. using bytes = std::string;
  70. using bytes_reader = __vic::string_reader;
  71. std::string encode_base16(const bytes &s)
  72. {
  73. std::string res;
  74. res.reserve(s.length() * 2);
  75. __vic::base16::encode_upper(bytes_reader(s), __vic::string_writer(res));
  76. return res;
  77. }
  78. ]]></code-block>
  79. </section>
  80. </chapter>
  81. <chapter xml:id="base16--encode_lower">
  82. <title><tt>base16::encode_lower()</tt></title>
  83. <code-block lang="C++"><![CDATA[
  84. // Bytes -> Text
  85. template<class ByteReader, class CharWriter>
  86. void base16::encode_lower(ByteReader reader, CharWriter writer);
  87. ]]></code-block>
  88. <p>Same as <xref to="base16--encode_upper"/> but lower case is used for
  89. hexadecimal digits.</p>
  90. </chapter>
  91. <chapter xml:id="base16--encode_byte_lower">
  92. <title><tt>base16::encode_byte_lower()</tt></title>
  93. <code-block lang="C++"><![CDATA[
  94. // Byte -> Text
  95. template<class CharWriter>
  96. void base16::encode_byte_lower(unsigned char byte, CharWriter writer);
  97. ]]></code-block>
  98. <p>Same as <xref to="base16--encode_lower"/> but encodes only single byte.</p>
  99. </chapter>
  100. <chapter xml:id="base16--encode_byte_upper">
  101. <title><tt>base16::encode_byte_upper()</tt></title>
  102. <code-block lang="C++"><![CDATA[
  103. // Byte -> Text
  104. template<class CharWriter>
  105. void base16::encode_byte_upper(unsigned char byte, CharWriter writer);
  106. ]]></code-block>
  107. <p>Same as <xref to="base16--encode_upper"/> but encodes only single byte.</p>
  108. </chapter>
  109. <chapter xml:id="base16--decode">
  110. <title><tt>base16::decode()</tt></title>
  111. <code-block lang="C++"><![CDATA[
  112. // Text -> Bytes
  113. template<class CharReader, class ByteWriter>
  114. void base16::decode(CharReader reader, ByteWriter writer);
  115. ]]></code-block>
  116. <p>Decodes characters from <tt>reader</tt> and writes the resulting bytes to
  117. <tt>writer</tt>. Exception derived from <xref to="base16--bad_format"/> is
  118. thrown if the input sequence has invalid Base16 format.</p>
  119. <p><tt>CharReader</tt> has to model <tt>Reader&lt;char></tt> concept.
  120. See <xref to="readers"/>.</p>
  121. <p><tt>ByteWriter</tt> has to model <tt>Writer&lt;unsigned char></tt> concept.
  122. See <xref to="writers"/>.</p>
  123. <section><title>Example</title>
  124. <code-block lang="C++"><![CDATA[
  125. #include<__vic/readers/string.h>
  126. #include<__vic/writers/string.h>
  127. using bytes = std::string;
  128. using bytes_writer = __vic::string_writer;
  129. bytes decode_base16(const std::string &s)
  130. {
  131. bytes res;
  132. res.reserve(s.length() / 2);
  133. __vic::base16::decode(__vic::string_reader(s), bytes_writer(res));
  134. return res;
  135. }
  136. ]]></code-block>
  137. </section>
  138. </chapter>
  139. <chapter xml:id="base16--try_decode">
  140. <title><tt>base16::try_decode()</tt></title>
  141. <code-block lang="C++"><![CDATA[
  142. // Text -> Bytes
  143. template<class CharReader, class ByteWriter>
  144. base16::status_t base16::try_decode(CharReader reader, ByteWriter writer);
  145. ]]></code-block>
  146. <p>Same as <xref to="base16--decode"/> but returns <xref to="base16--status"/>
  147. different from <tt>base16::status::ok</tt> in case of invalid input sequence
  148. instead of throwing exception.</p>
  149. </chapter>
  150. </chapter>