PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/modp_burl.h

http://stringencoders.googlecode.com/
C Header | 262 lines | 113 code | 33 blank | 116 comment | 0 complexity | fd50beb37951677719f53bbe8e1c1b8a MD5 | raw file
Possible License(s): BSD-3-Clause
  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 modp_burl.h
  5. * \brief high performance URL encoding and decoding
  6. *
  7. */
  8. /*
  9. * <PRE>
  10. * High Performance URL Encoder/Decoder
  11. *
  12. * Copyright &copy; 2006, 2007 Nick Galbreath -- nickg [at] client9 [dot] com
  13. * All rights reserved.
  14. *
  15. * http://code.google.com/p/stringencoders/
  16. *
  17. * Released under bsd license.
  18. * </PRE>
  19. */
  20. #ifndef COM_MODP_STRINGENCODERS_BURL
  21. #define COM_MODP_STRINGENCODERS_BURL
  22. /* size_t declaration */
  23. #include <stddef.h>
  24. #ifdef __cplusplus
  25. #ifndef MODP_C_BEGIN_DECLS
  26. # define MODP_C_BEGIN_DECLS extern "C" {
  27. # define MODP_C_END_DECLS }
  28. #endif
  29. #else
  30. # define MODP_C_BEGIN_DECLS
  31. # define MODP_C_END_DECLS
  32. #endif
  33. MODP_C_BEGIN_DECLS
  34. /**
  35. * Url encode a string. This uses a very strict definition of url
  36. * encoding. The only characters NOT encoded are A-Z, a-z, 0-9, "-",
  37. * "_", ".", along with the space char getting mapped to "+".
  38. * Everything else is escaped using "%HEXHEX" format. This is
  39. * identical to the implementation of php's urlencode and nearly
  40. * identical to Java's UrlEncoder class (they do not escape '*' for
  41. * some reason).
  42. *
  43. * \param[out] dest output string. Must
  44. * \param[in] str The input string
  45. * \param[in] len The length of the input string, excluding any
  46. * final null byte.
  47. */
  48. size_t modp_burl_encode(char* dest, const char* str, size_t len);
  49. /**
  50. * Url encode a string. This uses a minimal definition of url
  51. * encoding. This works similar to the previous function except '~',
  52. * '!', '$', '\'', '(', ')', '*', ',', ';', ':', '@', '/', '?' are NOT
  53. * escaped. This will allow decoding by standard url-decoders and
  54. * make the encoded urls more readable.
  55. *
  56. * \param[out] dest output string. Must
  57. * \param[in] str The input string
  58. * \param[in] len The length of the input string, excluding any
  59. * final null byte.
  60. */
  61. size_t modp_burl_min_encode(char* dest, const char* str, size_t len);
  62. /** \brief get size of output string w/o doing actual encoding
  63. *
  64. * \param[in] src input string, not null
  65. * \param[in] len length of input string
  66. * \return length of output string NOT including any final null byte
  67. */
  68. size_t modp_burl_min_encode_strlen(const char* src, const size_t len);
  69. /**
  70. * Provides the maximum size for output string given
  71. * and input size of A bytes.
  72. */
  73. #define modp_burl_encode_len(A) (3*A + 1)
  74. /**
  75. * Given the exact size of output string.
  76. *
  77. * Can be used to allocate the right amount of memory for
  78. * modp_burl_encode. Be sure to add 1 byte for final null.
  79. *
  80. * This is somewhat expensive since it examines every character
  81. * in the input string
  82. *
  83. * \param[in] str The input string
  84. * \param[in] len THe length of the input string, excluding any
  85. * final null byte (i.e. strlen(str))
  86. * \return the size of the output string, excluding the final
  87. * null byte.
  88. */
  89. size_t modp_burl_encode_strlen(const char* str, const size_t len);
  90. /**
  91. * URL Decode a string
  92. *
  93. * \param[out] dest The output string. Must be at least (len + 1)
  94. * bytes allocated. This may be the same as the input buffer.
  95. * \param[in] str The input string that is URL encoded.
  96. * \param[in] len The length of the input string (excluding final
  97. * null byte)
  98. * \return the strlen of the output string.
  99. */
  100. size_t modp_burl_decode(char* dest, const char* str, size_t len);
  101. /**
  102. * URL Decode a string, '+' is preserved
  103. *
  104. * \param[out] dest The output string. Must be at least (len + 1)
  105. * bytes allocated. This may be the same as the input buffer.
  106. * \param[in] str The input string that is URL encoded.
  107. * \param[in] len The length of the input string (excluding final
  108. * null byte)
  109. * \return the strlen of the output string.
  110. */
  111. size_t modp_burl_decode_raw(char* dest, const char* str, size_t len);
  112. /**
  113. * Returns memory required to decoded a url-encoded
  114. * string of length A.
  115. *
  116. */
  117. #define modp_burl_decode_len(A) (A + 1)
  118. MODP_C_END_DECLS
  119. #ifdef __cplusplus
  120. #include <cstring>
  121. #include <string>
  122. namespace modp {
  123. inline std::string url_encode(const char* s, size_t len)
  124. {
  125. std::string x(modp_burl_encode_len(len), '\0');
  126. size_t d = modp_burl_encode(const_cast<char*>(x.data()), s, len);
  127. x.erase(d, std::string::npos);
  128. return x;
  129. }
  130. inline std::string url_encode(const char* s)
  131. {
  132. return url_encode(s, strlen(s));
  133. }
  134. inline std::string url_encode(const std::string& s)
  135. {
  136. return url_encode(s.data(), s.size());
  137. }
  138. /**
  139. * Standard (maximal) url encoding.
  140. *
  141. * \param[in,out] s the string to be encoded
  142. * \return a reference to the input string
  143. */
  144. inline std::string& url_encode(std::string& s)
  145. {
  146. std::string x(url_encode(s.data(), s.size()));
  147. s.swap(x);
  148. return s;
  149. }
  150. /**
  151. * Minimal Url Encoding
  152. *
  153. * \param[in,out] s the string to be encoded
  154. * \return a reference to the input string
  155. */
  156. inline std::string& url_min_encode(std::string& s)
  157. {
  158. std::string x(modp_burl_encode_len(s.size()), '\0');
  159. size_t d = modp_burl_min_encode(const_cast<char*>(x.data()), s.data(), s.size());
  160. x.erase(d, std::string::npos);
  161. s.swap(x);
  162. return s;
  163. }
  164. inline std::string url_min_encode(const std::string& s)
  165. {
  166. std::string x(modp_burl_encode_len(s.size()), '\0');
  167. size_t d = modp_burl_min_encode(const_cast<char*>(x.data()), s.data(), s.size());
  168. x.erase(d, std::string::npos);
  169. return x;
  170. }
  171. /**
  172. * Url decode a string.
  173. * This function does not allocate memory.
  174. *
  175. * \param[in,out] s the string to be decoded
  176. * \return a reference to the input string.
  177. * There is no error case, bad characters are passed through
  178. */
  179. inline std::string& url_decode(std::string& s)
  180. {
  181. size_t d = modp_burl_decode(const_cast<char*>(s.data()), s.data(), s.size());
  182. s.erase(d, std::string::npos);
  183. return s;
  184. }
  185. inline std::string& url_decode_raw(std::string& s)
  186. {
  187. size_t d = modp_burl_decode_raw(const_cast<char*>(s.data()), s.data(), s.size());
  188. s.erase(d, std::string::npos);
  189. return s;
  190. }
  191. inline std::string url_decode(const char* str)
  192. {
  193. std::string s(str);
  194. url_decode(s);
  195. return s;
  196. }
  197. inline std::string url_decode_raw(const char* str)
  198. {
  199. std::string s(str);
  200. url_decode_raw(s);
  201. return s;
  202. }
  203. inline std::string url_decode(const char* str, size_t len)
  204. {
  205. std::string s(str, len);
  206. url_decode(s);
  207. return s;
  208. }
  209. inline std::string url_decode_raw(const char* str, size_t len)
  210. {
  211. std::string s(str, len);
  212. url_decode_raw(s);
  213. return s;
  214. }
  215. inline std::string url_decode(const std::string& s)
  216. {
  217. std::string x(s);
  218. url_decode(x);
  219. return x;
  220. }
  221. inline std::string url_decode_raw(const std::string& s)
  222. {
  223. std::string x(s);
  224. url_decode_raw(x);
  225. return x;
  226. }
  227. }
  228. #endif
  229. #endif