modp_b64.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include <stddef.h>
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. * Encode a raw binary string into base 64.
  30. * src contains the bytes
  31. * len contains the number of bytes in the src
  32. * dest should be allocated by the caller to contain
  33. * at least modp_b64_encode_len(len) bytes (see below)
  34. * This will contain the null-terminated b64 encoded result
  35. * returns length of the destination string plus the ending null byte
  36. * i.e. the result will be equal to strlen(dest) + 1
  37. *
  38. * Example
  39. *
  40. * \code
  41. * char* src = ...;
  42. * int srclen = ...; //the length of number of bytes in src
  43. * char* dest = (char*) malloc(modp_b64_encode_len);
  44. * int len = modp_b64_encode(dest, src, sourcelen);
  45. * if (len == -1) {
  46. * printf("Error\n");
  47. * } else {
  48. * printf("b64 = %s\n", dest);
  49. * }
  50. * \endcode
  51. *
  52. */
  53. size_t modp_b64_encode(char* dest, const char* str, size_t len);
  54. /**
  55. * Decode a base64 encoded string
  56. *
  57. * src should contain exactly len bytes of b64 characters.
  58. * if src contains -any- non-base characters (such as white
  59. * space, -1 is returned.
  60. *
  61. * dest should be allocated by the caller to contain at least
  62. * len * 3 / 4 bytes.
  63. *
  64. * Returns the length (strlen) of the output, or -1 if unable to
  65. * decode
  66. *
  67. * \code
  68. * char* src = ...;
  69. * int srclen = ...; // or if you don't know use strlen(src)
  70. * char* dest = (char*) malloc(modp_b64_decode_len(srclen));
  71. * int len = modp_b64_decode(dest, src, sourcelen);
  72. * if (len == -1) { error }
  73. * \endcode
  74. */
  75. size_t modp_b64_decode(char* dest, const char* src, size_t len);
  76. /**
  77. * Given a source string of length len, this returns the amount of
  78. * memory the destination string should have.
  79. *
  80. * remember, this is integer math
  81. * 3 bytes turn into 4 chars
  82. * ceiling[len / 3] * 4 + 1
  83. *
  84. * +1 is for any extra null.
  85. */
  86. #define modp_b64_encode_len(A) ((A+2)/3 * 4 + 1)
  87. /**
  88. * Given a base64 string of length len,
  89. * this returns the amount of memory required for output string
  90. * It maybe be more than the actual number of bytes written.
  91. * NOTE: remember this is integer math
  92. * this allocates a bit more memory than traditional versions of b64
  93. * decode 4 chars turn into 3 bytes
  94. * floor[len * 3/4] + 2
  95. */
  96. #define modp_b64_decode_len(A) (A / 4 * 3 + 2)
  97. /**
  98. * Will return the strlen of the output from encoding.
  99. * This may be less than the required number of bytes allocated.
  100. *
  101. * This allows you to 'deserialized' a struct
  102. * \code
  103. * char* b64encoded = "...";
  104. * int len = strlen(b64encoded);
  105. *
  106. * struct datastuff foo;
  107. * if (modp_b64_encode_strlen(sizeof(struct datastuff)) != len) {
  108. * // wrong size
  109. * return false;
  110. * } else {
  111. * // safe to do;
  112. * if (modp_b64_decode((char*) &foo, b64encoded, len) == -1) {
  113. * // bad characters
  114. * return false;
  115. * }
  116. * }
  117. * // foo is filled out now
  118. * \endcode
  119. */
  120. #define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)
  121. #define MODP_B64_ERROR ((size_t)-1)
  122. #ifdef __cplusplus
  123. }
  124. #include <string>
  125. inline std::string& modp_b64_encode(std::string& s)
  126. {
  127. std::string x(modp_b64_encode_len(s.size()), '\0');
  128. size_t d = modp_b64_encode(const_cast<char*>(x.data()), s.data(), s.size());
  129. x.erase(d, std::string::npos);
  130. s.swap(x);
  131. return s;
  132. }
  133. /**
  134. * base 64 decode a string (self-modifing)
  135. * On failure, the string is empty.
  136. *
  137. * This function is for C++ only (duh)
  138. *
  139. * \param[in,out] s the string to be decoded
  140. * \return a reference to the input string
  141. */
  142. inline std::string& modp_b64_decode(std::string& s)
  143. {
  144. std::string x(modp_b64_decode_len(s.size()), '\0');
  145. size_t d = modp_b64_decode(const_cast<char*>(x.data()), s.data(), s.size());
  146. if (d == MODP_B64_ERROR) {
  147. x.clear();
  148. } else {
  149. x.erase(d, std::string::npos);
  150. }
  151. s.swap(x);
  152. return s;
  153. }
  154. #endif /* __cplusplus */
  155. #endif /* MODP_B64 */