base64.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/base64.h
  3. // Purpose: declaration of BASE64 encoding/decoding functionality
  4. // Author: Charles Reimers, Vadim Zeitlin
  5. // Created: 2007-06-18
  6. // Licence: wxWindows licence
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef _WX_BASE64_H_
  9. #define _WX_BASE64_H_
  10. #if wxUSE_BASE64
  11. #include "wx/string.h"
  12. #include "wx/buffer.h"
  13. // ----------------------------------------------------------------------------
  14. // encoding functions
  15. // ----------------------------------------------------------------------------
  16. // return the size needed for the buffer containing the encoded representation
  17. // of a buffer of given length
  18. inline size_t wxBase64EncodedSize(size_t len) { return 4*((len+2)/3); }
  19. // raw base64 encoding function which encodes the contents of a buffer of the
  20. // specified length into the buffer of the specified size
  21. //
  22. // returns the length of the encoded data or wxCONV_FAILED if the buffer is not
  23. // large enough; to determine the needed size you can either allocate a buffer
  24. // of wxBase64EncodedSize(srcLen) size or call the function with NULL buffer in
  25. // which case the required size will be returned
  26. WXDLLIMPEXP_BASE size_t
  27. wxBase64Encode(char *dst, size_t dstLen, const void *src, size_t srcLen);
  28. // encode the contents of the given buffer using base64 and return as string
  29. // (there is no error return)
  30. inline wxString wxBase64Encode(const void *src, size_t srcLen)
  31. {
  32. const size_t dstLen = wxBase64EncodedSize(srcLen);
  33. wxCharBuffer dst(dstLen);
  34. wxBase64Encode(dst.data(), dstLen, src, srcLen);
  35. return dst;
  36. }
  37. inline wxString wxBase64Encode(const wxMemoryBuffer& buf)
  38. {
  39. return wxBase64Encode(buf.GetData(), buf.GetDataLen());
  40. }
  41. // ----------------------------------------------------------------------------
  42. // decoding functions
  43. // ----------------------------------------------------------------------------
  44. // elements of this enum specify the possible behaviours of wxBase64Decode()
  45. // when an invalid character is encountered
  46. enum wxBase64DecodeMode
  47. {
  48. // normal behaviour: stop at any invalid characters
  49. wxBase64DecodeMode_Strict,
  50. // skip whitespace characters
  51. wxBase64DecodeMode_SkipWS,
  52. // the most lenient behaviour: simply ignore all invalid characters
  53. wxBase64DecodeMode_Relaxed
  54. };
  55. // return the buffer size necessary for decoding a base64 string of the given
  56. // length
  57. inline size_t wxBase64DecodedSize(size_t srcLen) { return 3*srcLen/4; }
  58. // raw decoding function which decodes the contents of the string of specified
  59. // length (or NUL-terminated by default) into the provided buffer of the given
  60. // size
  61. //
  62. // the function normally stops at any character invalid inside a base64-encoded
  63. // string (i.e. not alphanumeric nor '+' nor '/') but can be made to skip the
  64. // whitespace or all invalid characters using its mode argument
  65. //
  66. // returns the length of the decoded data or wxCONV_FAILED if an error occurs
  67. // such as the buffer is too small or the encoded string is invalid; in the
  68. // latter case the posErr is filled with the position where the decoding
  69. // stopped if it is not NULL
  70. WXDLLIMPEXP_BASE size_t
  71. wxBase64Decode(void *dst, size_t dstLen,
  72. const char *src, size_t srcLen = wxNO_LEN,
  73. wxBase64DecodeMode mode = wxBase64DecodeMode_Strict,
  74. size_t *posErr = NULL);
  75. inline size_t
  76. wxBase64Decode(void *dst, size_t dstLen,
  77. const wxString& src,
  78. wxBase64DecodeMode mode = wxBase64DecodeMode_Strict,
  79. size_t *posErr = NULL)
  80. {
  81. // don't use str.length() here as the ASCII buffer is shorter than it for
  82. // strings with embedded NULs
  83. return wxBase64Decode(dst, dstLen, src.ToAscii(), wxNO_LEN, mode, posErr);
  84. }
  85. // decode the contents of the given string; the returned buffer is empty if an
  86. // error occurs during decoding
  87. WXDLLIMPEXP_BASE wxMemoryBuffer
  88. wxBase64Decode(const char *src, size_t srcLen = wxNO_LEN,
  89. wxBase64DecodeMode mode = wxBase64DecodeMode_Strict,
  90. size_t *posErr = NULL);
  91. inline wxMemoryBuffer
  92. wxBase64Decode(const wxString& src,
  93. wxBase64DecodeMode mode = wxBase64DecodeMode_Strict,
  94. size_t *posErr = NULL)
  95. {
  96. // don't use str.length() here as the ASCII buffer is shorter than it for
  97. // strings with embedded NULs
  98. return wxBase64Decode(src.ToAscii(), wxNO_LEN, mode, posErr);
  99. }
  100. #endif // wxUSE_BASE64
  101. #endif // _WX_BASE64_H_