coding.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*!
  2. \ingroup Base_Encoding
  3. \brief This function decodes the given Base64 encoded input, in, and
  4. stores the result in the output buffer out. It also sets the size
  5. written to the output buffer in the variable outLen.
  6. \return 0 Returned upon successfully decoding the Base64 encoded input
  7. \return BAD_FUNC_ARG Returned if the output buffer is too small to
  8. store the decoded input
  9. \return ASN_INPUT_E Returned if a character in the input buffer falls
  10. outside of the Base64 range ([A-Za-z0-9+/=]) or if there is an invalid
  11. line ending in the Base64 encoded input
  12. \param in pointer to the input buffer to decode
  13. \param inLen length of the input buffer to decode
  14. \param out pointer to the output buffer in which to store the decoded
  15. message
  16. \param outLen pointer to the length of the output buffer. Updated with
  17. the bytes written at the end of the function call
  18. _Example_
  19. \code
  20. byte encoded[] = { // initialize text to decode };
  21. byte decoded[sizeof(encoded)];
  22. // requires at least (sizeof(encoded) * 3 + 3) / 4 room
  23. int outLen = sizeof(decoded);
  24. if( Base64_Decode(encoded,sizeof(encoded), decoded, &outLen) != 0 ) {
  25. // error decoding input buffer
  26. }
  27. \endcode
  28. \sa Base64_Encode
  29. \sa Base16_Decode
  30. */
  31. WOLFSSL_API int Base64_Decode(const byte* in, word32 inLen, byte* out,
  32. word32* outLen);
  33. /*!
  34. \ingroup Base_Encoding
  35. \brief This function encodes the given input, in, and stores the Base64
  36. encoded result in the output buffer out. It writes the data with the
  37. traditional ‘\n’ line endings, instead of escaped %0A line endings. Upon
  38. successfully completing, this function also sets outLen to the number
  39. of bytes written to the output buffer.
  40. \return 0 Returned upon successfully decoding the Base64 encoded input
  41. \return BAD_FUNC_ARG Returned if the output buffer is too small to
  42. store the encoded input
  43. \return BUFFER_E Returned if the output buffer runs out of room
  44. while encoding
  45. \param in pointer to the input buffer to encode
  46. \param inLen length of the input buffer to encode
  47. \param out pointer to the output buffer in which to store the
  48. encoded message
  49. \param outLen pointer to the length of the output buffer in
  50. which to store the encoded message
  51. _Example_
  52. \code
  53. byte plain[] = { // initialize text to encode };
  54. byte encoded[MAX_BUFFER_SIZE];
  55. int outLen = sizeof(encoded);
  56. if( Base64_Encode(plain, sizeof(plain), encoded, &outLen) != 0 ) {
  57. // error encoding input buffer
  58. }
  59. \endcode
  60. \sa Base64_EncodeEsc
  61. \sa Base64_Decode
  62. */
  63. WOLFSSL_API
  64. int Base64_Encode(const byte* in, word32 inLen, byte* out,
  65. word32* outLen);
  66. /*!
  67. \ingroup Base_Encoding
  68. \brief This function encodes the given input, in, and stores the
  69. Base64 encoded result in the output buffer out. It writes the data
  70. with %0A escaped line endings instead of ‘\n’ line endings.
  71. Upon successfully completing, this function also sets outLen
  72. to the number of bytes written to the output buffer.
  73. \return 0 Returned upon successfully decoding the Base64 encoded input
  74. \return BAD_FUNC_ARG Returned if the output buffer is too small
  75. to store the encoded input
  76. \return BUFFER_E Returned if the output buffer runs out of
  77. room while encoding
  78. \return ASN_INPUT_E Returned if there is an error processing
  79. the decode on the input message
  80. \param in pointer to the input buffer to encode
  81. \param inLen length of the input buffer to encode
  82. \param out pointer to the output buffer in which to store
  83. the encoded message
  84. \param outLen pointer to the length of the output buffer in
  85. which to store the encoded message
  86. _Example_
  87. \code
  88. byte plain[] = { // initialize text to encode };
  89. byte encoded[MAX_BUFFER_SIZE];
  90. int outLen = sizeof(encoded);
  91. if( Base64_EncodeEsc(plain, sizeof(plain), encoded, &outLen) != 0 ) {
  92. // error encoding input buffer
  93. }
  94. \endcode
  95. \sa Base64_Encode
  96. \sa Base64_Decode
  97. */
  98. int Base64_EncodeEsc(const byte* in, word32 inLen, byte* out,
  99. word32* outLen);
  100. /*!
  101. \ingroup Base_Encoding
  102. \brief This function encodes the given input, in, and stores the
  103. Base64 encoded result in the output buffer out. It writes the data
  104. with no new lines. Upon successfully completing, this function
  105. also sets outLen to the number of bytes written to the output buffer
  106. \return 0 Returned upon successfully decoding the Base64 encoded input
  107. \return BAD_FUNC_ARG Returned if the output buffer is too small
  108. to store the encoded input
  109. \return BUFFER_E Returned if the output buffer runs out of room
  110. while encoding
  111. \return ASN_INPUT_E Returned if there is an error processing the
  112. decode on the input message
  113. \param in pointer to the input buffer to encode
  114. \param inLen length of the input buffer to encode
  115. \param out pointer to the output buffer in which to store the
  116. encoded message
  117. \param outLen pointer to the length of the output buffer in which to
  118. store the encoded message
  119. _Example_
  120. \code
  121. byte plain[] = { // initialize text to encode };
  122. byte encoded[MAX_BUFFER_SIZE];
  123. int outLen = sizeof(encoded);
  124. if( Base64_Encode_NoNl(plain, sizeof(plain), encoded, &outLen) != 0 ) {
  125. // error encoding input buffer
  126. }
  127. \endcode
  128. \sa Base64_Encode
  129. \sa Base64_Decode
  130. */
  131. WOLFSSL_API
  132. int Base64_Encode_NoNl(const byte* in, word32 inLen, byte* out,
  133. word32* outLen);
  134. /*!
  135. \ingroup Base_Encoding
  136. \brief This function decodes the given Base16 encoded input, in, and
  137. stores the result in the output buffer out. It also sets the size written
  138. to the output buffer in the variable outLen.
  139. \return 0 Returned upon successfully decoding the Base16 encoded input
  140. \return BAD_FUNC_ARG Returned if the output buffer is too small to store
  141. the decoded input or if the input length is not a multiple of two
  142. \return ASN_INPUT_E Returned if a character in the input buffer falls
  143. outside of the Base16 range ([0-9A-F])
  144. \param in pointer to the input buffer to decode
  145. \param inLen length of the input buffer to decode
  146. \param out pointer to the output buffer in which to store the decoded
  147. message
  148. \param outLen pointer to the length of the output buffer. Updated with the
  149. bytes written at the end of the function call
  150. _Example_
  151. \code
  152. byte encoded[] = { // initialize text to decode };
  153. byte decoded[sizeof(encoded)];
  154. int outLen = sizeof(decoded);
  155. if( Base16_Decode(encoded,sizeof(encoded), decoded, &outLen) != 0 ) {
  156. // error decoding input buffer
  157. }
  158. \endcode
  159. \sa Base64_Encode
  160. \sa Base64_Decode
  161. \sa Base16_Encode
  162. */
  163. WOLFSSL_API
  164. int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen);
  165. /*!
  166. \ingroup Base_Encoding
  167. \brief Encode input to base16 output.
  168. \return 0 Success
  169. \return BAD_FUNC_ARG Returns if in, out, or outLen is null or if outLen is
  170. less than 2 times inLen plus 1.
  171. \param in Pointer to input buffer to be encoded.
  172. \param inLen Length of input buffer.
  173. \param out Pointer to output buffer.
  174. \param outLen Length of output buffer. Is set to len of encoded output.
  175. _Example_
  176. \code
  177. byte in[] = { // Contents of something to be encoded };
  178. byte out[NECESSARY_OUTPUT_SIZE];
  179. word32 outSz = sizeof(out);
  180. if(Base16_Encode(in, sizeof(in), out, &outSz) != 0)
  181. {
  182. // Handle encode error
  183. }
  184. \endcode
  185. \sa Base64_Encode
  186. \sa Base64_Decode
  187. \sa Base16_Decode
  188. */
  189. WOLFSSL_API
  190. int Base16_Encode(const byte* in, word32 inLen, byte* out, word32* outLen);