base64.h.xml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <chapter xml:id="base64.h">
  2. <title><tt>__vic/base64.h</tt></title>
  3. <p>Base64 encoder and decoder.</p>
  4. <chapter xml:id="base64">
  5. <title><tt>base64</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="base64--bad_format">
  10. <title><tt>base64::bad_format</tt></title>
  11. <code-block lang="C++">
  12. struct base64::bad_format : public std::exception {};
  13. </code-block>
  14. <p>Abstract base exception class.</p>
  15. </chapter>
  16. <chapter xml:id="base64--bad_digit">
  17. <title><tt>base64::bad_digit</tt></title>
  18. <code-block lang="C++">
  19. struct base64::bad_digit : public base64::bad_format
  20. {
  21. const char *what() const noexcept;
  22. };
  23. </code-block>
  24. <p>Exception class thrown by <xref to="base64--decode"/> when the input sequence
  25. contains character that is not a valid Base64 digit.</p>
  26. </chapter>
  27. <chapter xml:id="base64--bad_length">
  28. <title><tt>base64::bad_length</tt></title>
  29. <code-block lang="C++">
  30. struct base64::bad_length : public base64::bad_format
  31. {
  32. const char *what() const noexcept;
  33. };
  34. </code-block>
  35. <p>Exception class thrown by <xref to="base64--decode"/> when the input sequence
  36. length is not a multiple of 4.</p>
  37. </chapter>
  38. <chapter xml:id="base64--status">
  39. <title><tt>base64::status</tt></title>
  40. <code-block lang="C++">
  41. enum class base64::status
  42. {
  43. ok,
  44. invalid_length,
  45. invalid_digit
  46. };
  47. using base64::status_t = base64::status; // for C++98
  48. </code-block>
  49. <p>Input sequence parsing outcome status codes returned by
  50. <xref to="base64--try_decode"/>.</p>
  51. </chapter>
  52. <chapter xml:id="base64--encode">
  53. <title><tt>base64::encode()</tt></title>
  54. <code-block lang="C++"><![CDATA[
  55. // Bytes -> Text
  56. template<class ByteReader, class CharWriter>
  57. void base64::encode(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>.</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_base64(const bytes &s)
  72. {
  73. std::string res;
  74. res.reserve(__vic::base64::encoded_length(s.length()));
  75. __vic::base64::encode(bytes_reader(s), __vic::string_writer(res));
  76. return res;
  77. }
  78. ]]></code-block>
  79. </section>
  80. </chapter>
  81. <chapter xml:id="base64--decode">
  82. <title><tt>base64::decode()</tt></title>
  83. <code-block lang="C++"><![CDATA[
  84. // Text -> Bytes
  85. template<class CharReader, class ByteWriter>
  86. void base64::decode(CharReader reader, ByteWriter writer);
  87. ]]></code-block>
  88. <p>Decodes characters form <tt>reader</tt> and writes the resulting bytes to
  89. <tt>writer</tt>. Exception derived from <xref to="base64--bad_format"/> is
  90. thrown if the input sequence has invalid Base64 format.</p>
  91. <p><tt>CharReader</tt> has to model <tt>Reader&lt;char></tt> concept.
  92. See <xref to="readers"/>.</p>
  93. <p><tt>ByteWriter</tt> has to model <tt>Writer&lt;unsigned char></tt> concept.
  94. See <xref to="writers"/>.</p>
  95. <section><title>Example</title>
  96. <code-block lang="C++"><![CDATA[
  97. #include<__vic/readers/string.h>
  98. #include<__vic/writers/string.h>
  99. using bytes = std::string;
  100. using bytes_writer = __vic::string_writer;
  101. bytes decode_base64(const std::string &s)
  102. {
  103. bytes res;
  104. res.reserve(__vic::base64::max_decoded_length(s.length()));
  105. __vic::base64::decode(__vic::string_reader(s), bytes_writer(res));
  106. return res;
  107. }
  108. ]]></code-block>
  109. </section>
  110. </chapter>
  111. <chapter xml:id="base64--try_decode">
  112. <title><tt>base64::try_decode()</tt></title>
  113. <code-block lang="C++"><![CDATA[
  114. // Text -> Bytes
  115. template<class CharReader, class ByteWriter>
  116. base64::status_t base64::try_decode(CharReader reader, ByteWriter writer);
  117. ]]></code-block>
  118. <p>Same as <xref to="base64--decode"/> but returns <xref to="base64--status"/>
  119. different from <tt>base64::status::ok</tt> in case of invalid input sequence
  120. instead of throwing exception.</p>
  121. </chapter>
  122. <chapter xml:id="base64--encoded_length">
  123. <title><tt>base64::encoded_length()</tt></title>
  124. <code-block lang="C++">
  125. constexpr size_t base64::encoded_length(size_t orig_len);
  126. </code-block>
  127. <p>Calculates the length of encoded sequence of characters using the original
  128. length of the bytes sequence.</p>
  129. </chapter>
  130. <chapter xml:id="base64--max_decoded_length">
  131. <title><tt>base64::max_decoded_length()</tt></title>
  132. <code-block lang="C++">
  133. constexpr size_t base64::max_decoded_length(size_t orig_len);
  134. </code-block>
  135. <p>Estimates the maximum length of decoded sequence of bytes using the original
  136. length of the characters sequence. The actual value depends on trailing
  137. <tt>'='</tt> in the encoded value.</p>
  138. </chapter>
  139. </chapter>