modp_b64.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
  2. /* vi: set expandtab shiftwidth=4 tabstop=4: */
  3. /**
  4. * \file
  5. * <PRE>
  6. * High performance base64 encoder / decoder
  7. * Version 1.3 -- 17-Mar-2006
  8. *
  9. * Copyright &copy; 2005, 2006, Nick Galbreath -- nickg [at] modp [dot] com
  10. * All rights reserved.
  11. *
  12. * http://modp.com/release/base64
  13. *
  14. * Released under bsd license. See modp_b64.c for details.
  15. * </pre>
  16. *
  17. * The default implementation is the standard b64 encoding with padding.
  18. * It's easy to change this to use "URL safe" characters and to remove
  19. * padding. See the modp_b64.c source code for details.
  20. *
  21. */
  22. #ifndef MODP_B64
  23. #define MODP_B64
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * Encode a raw binary string into base 64.
  29. * src contains the bytes
  30. * len contains the number of bytes in the src
  31. * dest should be allocated by the caller to contain
  32. * at least modp_b64_encode_len(len) bytes (see below)
  33. * This will contain the null-terminated b64 encoded result
  34. * returns length of the destination string plus the ending null byte
  35. * i.e. the result will be equal to strlen(dest) + 1
  36. *
  37. * Example
  38. *
  39. * \code
  40. * char* src = ...;
  41. * int srclen = ...; //the length of number of bytes in src
  42. * char* dest = (char*) malloc(modp_b64_encode_len);
  43. * int len = modp_b64_encode(dest, src, sourcelen);
  44. * if (len == -1) {
  45. * printf("Error\n");
  46. * } else {
  47. * printf("b64 = %s\n", dest);
  48. * }
  49. * \endcode
  50. *
  51. */
  52. int modp_b64_encode(char* dest, const char* str, int len);
  53. /**
  54. * Decode a base64 encoded string
  55. *
  56. * src should contain exactly len bytes of b64 characters.
  57. * if src contains -any- non-base characters (such as white
  58. * space, -1 is returned.
  59. *
  60. * dest should be allocated by the caller to contain at least
  61. * len * 3 / 4 bytes.
  62. *
  63. * Returns the length (strlen) of the output, or -1 if unable to
  64. * decode
  65. *
  66. * \code
  67. * char* src = ...;
  68. * int srclen = ...; // or if you don't know use strlen(src)
  69. * char* dest = (char*) malloc(modp_b64_decode_len(srclen));
  70. * int len = modp_b64_decode(dest, src, sourcelen);
  71. * if (len == -1) { error }
  72. * \endcode
  73. */
  74. int modp_b64_decode(char* dest, const char* src, int len);
  75. /**
  76. * Given a source string of length len, this returns the amount of
  77. * memory the destination string should have.
  78. *
  79. * remember, this is integer math
  80. * 3 bytes turn into 4 chars
  81. * ceiling[len / 3] * 4 + 1
  82. *
  83. * +1 is for any extra null.
  84. */
  85. #define modp_b64_encode_len(A) ((A+2)/3 * 4 + 1)
  86. /**
  87. * Given a base64 string of length len,
  88. * this returns the amount of memory required for output string
  89. * It maybe be more than the actual number of bytes written.
  90. * NOTE: remember this is integer math
  91. * this allocates a bit more memory than traditional versions of b64
  92. * decode 4 chars turn into 3 bytes
  93. * floor[len * 3/4] + 2
  94. */
  95. #define modp_b64_decode_len(A) (A / 4 * 3 + 2)
  96. /**
  97. * Will return the strlen of the output from encoding.
  98. * This may be less than the required number of bytes allocated.
  99. *
  100. * This allows you to 'deserialized' a struct
  101. * \code
  102. * char* b64encoded = "...";
  103. * int len = strlen(b64encoded);
  104. *
  105. * struct datastuff foo;
  106. * if (modp_b64_encode_strlen(sizeof(struct datastuff)) != len) {
  107. * // wrong size
  108. * return false;
  109. * } else {
  110. * // safe to do;
  111. * if (modp_b64_decode((char*) &foo, b64encoded, len) == -1) {
  112. * // bad characters
  113. * return false;
  114. * }
  115. * }
  116. * // foo is filled out now
  117. * \endcode
  118. */
  119. #define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)
  120. #ifdef __cplusplus
  121. }
  122. #include <string>
  123. inline std::string& modp_b64_encode(std::string& s)
  124. {
  125. std::string x(modp_b64_encode_len(s.size()), '\0');
  126. int d = modp_b64_encode(const_cast<char*>(x.data()), s.data(), s.size());
  127. x.erase(d, std::string::npos);
  128. s.swap(x);
  129. return s;
  130. }
  131. /**
  132. * base 64 decode a string (self-modifing)
  133. * On failure, the string is empty.
  134. *
  135. * This function is for C++ only (duh)
  136. *
  137. * \param[in,out] s the string to be decoded
  138. * \return a reference to the input string
  139. */
  140. inline std::string& modp_b64_decode(std::string& s)
  141. {
  142. std::string x(modp_b64_decode_len(s.size()), '\0');
  143. int d = modp_b64_decode(const_cast<char*>(x.data()), s.data(), s.size());
  144. if (d < 0) {
  145. x.clear();
  146. } else {
  147. x.erase(d, std::string::npos);
  148. }
  149. s.swap(x);
  150. return s;
  151. }
  152. #endif /* __cplusplus */
  153. #endif /* MODP_B64 */