protocol_examples_utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Utility functions for protocol examples
  3. *
  4. * SPDX-FileCopyrightText: 2002-2021 Igor Sysoev
  5. * 2011-2022 Nginx, Inc.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. *
  9. * SPDX-FileContributor: 2023 Espressif Systems (Shanghai) CO LTD
  10. */
  11. /*
  12. * Copyright (C) 2002-2021 Igor Sysoev
  13. * Copyright (C) 2011-2022 Nginx, Inc.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include "protocol_examples_utils.h"
  42. /* Type of Escape algorithms to be used */
  43. #define NGX_ESCAPE_URI (0)
  44. #define NGX_ESCAPE_ARGS (1)
  45. #define NGX_ESCAPE_URI_COMPONENT (2)
  46. #define NGX_ESCAPE_HTML (3)
  47. #define NGX_ESCAPE_REFRESH (4)
  48. #define NGX_ESCAPE_MEMCACHED (5)
  49. #define NGX_ESCAPE_MAIL_AUTH (6)
  50. /* Type of Unescape algorithms to be used */
  51. #define NGX_UNESCAPE_URI (1)
  52. #define NGX_UNESCAPE_REDIRECT (2)
  53. uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size, unsigned int type)
  54. {
  55. unsigned int n;
  56. uint32_t *escape;
  57. static u_char hex[] = "0123456789ABCDEF";
  58. /*
  59. * Per RFC 3986 only the following chars are allowed in URIs unescaped:
  60. *
  61. * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  62. * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
  63. * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  64. * / "*" / "+" / "," / ";" / "="
  65. *
  66. * And "%" can appear as a part of escaping itself. The following
  67. * characters are not allowed and need to be escaped: %00-%1F, %7F-%FF,
  68. * " ", """, "<", ">", "\", "^", "`", "{", "|", "}".
  69. */
  70. /* " ", "#", "%", "?", not allowed */
  71. static uint32_t uri[] = {
  72. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  73. /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
  74. 0xd000002d, /* 1101 0000 0000 0000 0000 0000 0010 1101 */
  75. /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
  76. 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */
  77. /* ~}| {zyx wvut srqp onml kjih gfed cba` */
  78. 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
  79. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  80. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  81. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  82. 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  83. };
  84. /* " ", "#", "%", "&", "+", ";", "?", not allowed */
  85. static uint32_t args[] = {
  86. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  87. /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
  88. 0xd800086d, /* 1101 1000 0000 0000 0000 1000 0110 1101 */
  89. /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
  90. 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */
  91. /* ~}| {zyx wvut srqp onml kjih gfed cba` */
  92. 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
  93. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  94. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  95. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  96. 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  97. };
  98. /* not ALPHA, DIGIT, "-", ".", "_", "~" */
  99. static uint32_t uri_component[] = {
  100. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  101. /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
  102. 0xfc009fff, /* 1111 1100 0000 0000 1001 1111 1111 1111 */
  103. /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
  104. 0x78000001, /* 0111 1000 0000 0000 0000 0000 0000 0001 */
  105. /* ~}| {zyx wvut srqp onml kjih gfed cba` */
  106. 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
  107. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  108. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  109. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  110. 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  111. };
  112. /* " ", "#", """, "%", "'", not allowed */
  113. static uint32_t html[] = {
  114. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  115. /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
  116. 0x500000ad, /* 0101 0000 0000 0000 0000 0000 1010 1101 */
  117. /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
  118. 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */
  119. /* ~}| {zyx wvut srqp onml kjih gfed cba` */
  120. 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
  121. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  122. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  123. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  124. 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  125. };
  126. /* " ", """, "'", not allowed */
  127. static uint32_t refresh[] = {
  128. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  129. /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
  130. 0x50000085, /* 0101 0000 0000 0000 0000 0000 1000 0101 */
  131. /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
  132. 0x50000000, /* 0101 0000 0000 0000 0000 0000 0000 0000 */
  133. /* ~}| {zyx wvut srqp onml kjih gfed cba` */
  134. 0xd8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
  135. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  136. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  137. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  138. 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  139. };
  140. /* " ", "%", %00-%1F */
  141. static uint32_t memcached[] = {
  142. 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
  143. /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
  144. 0x00000021, /* 0000 0000 0000 0000 0000 0000 0010 0001 */
  145. /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
  146. 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
  147. /* ~}| {zyx wvut srqp onml kjih gfed cba` */
  148. 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
  149. 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
  150. 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
  151. 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
  152. 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
  153. };
  154. /* mail_auth is the same as memcached */
  155. static uint32_t *map[] =
  156. { uri, args, uri_component, html, refresh, memcached, memcached };
  157. escape = map[type];
  158. if (dst == NULL) {
  159. /* find the number of the characters to be escaped */
  160. n = 0;
  161. while (size) {
  162. if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
  163. n++;
  164. }
  165. src++;
  166. size--;
  167. }
  168. return (uintptr_t) n;
  169. }
  170. while (size) {
  171. if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
  172. *dst++ = '%';
  173. *dst++ = hex[*src >> 4];
  174. *dst++ = hex[*src & 0xf];
  175. src++;
  176. } else {
  177. *dst++ = *src++;
  178. }
  179. size--;
  180. }
  181. return (uintptr_t) dst;
  182. }
  183. void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, unsigned int type)
  184. {
  185. u_char *d, *s, ch, c, decoded;
  186. enum {
  187. sw_usual = 0,
  188. sw_quoted,
  189. sw_quoted_second
  190. } state;
  191. d = *dst;
  192. s = *src;
  193. state = 0;
  194. decoded = 0;
  195. while (size--) {
  196. ch = *s++;
  197. switch (state) {
  198. case sw_usual:
  199. if (ch == '?'
  200. && (type & (NGX_UNESCAPE_URI | NGX_UNESCAPE_REDIRECT))) {
  201. *d++ = ch;
  202. goto done;
  203. }
  204. if (ch == '%') {
  205. state = sw_quoted;
  206. break;
  207. }
  208. *d++ = ch;
  209. break;
  210. case sw_quoted:
  211. if (ch >= '0' && ch <= '9') {
  212. decoded = (u_char) (ch - '0');
  213. state = sw_quoted_second;
  214. break;
  215. }
  216. c = (u_char) (ch | 0x20);
  217. if (c >= 'a' && c <= 'f') {
  218. decoded = (u_char) (c - 'a' + 10);
  219. state = sw_quoted_second;
  220. break;
  221. }
  222. /* the invalid quoted character */
  223. state = sw_usual;
  224. *d++ = ch;
  225. break;
  226. case sw_quoted_second:
  227. state = sw_usual;
  228. if (ch >= '0' && ch <= '9') {
  229. ch = (u_char) ((decoded << 4) + (ch - '0'));
  230. if (type & NGX_UNESCAPE_REDIRECT) {
  231. if (ch > '%' && ch < 0x7f) {
  232. *d++ = ch;
  233. break;
  234. }
  235. *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
  236. break;
  237. }
  238. *d++ = ch;
  239. break;
  240. }
  241. c = (u_char) (ch | 0x20);
  242. if (c >= 'a' && c <= 'f') {
  243. ch = (u_char) ((decoded << 4) + (c - 'a') + 10);
  244. if (type & NGX_UNESCAPE_URI) {
  245. if (ch == '?') {
  246. *d++ = ch;
  247. goto done;
  248. }
  249. *d++ = ch;
  250. break;
  251. }
  252. if (type & NGX_UNESCAPE_REDIRECT) {
  253. if (ch == '?') {
  254. *d++ = ch;
  255. goto done;
  256. }
  257. if (ch > '%' && ch < 0x7f) {
  258. *d++ = ch;
  259. break;
  260. }
  261. *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
  262. break;
  263. }
  264. *d++ = ch;
  265. break;
  266. }
  267. /* the invalid quoted character */
  268. break;
  269. }
  270. }
  271. done:
  272. *dst = d;
  273. *src = s;
  274. }
  275. uint32_t example_uri_encode(char *dest, const char *src, size_t len)
  276. {
  277. if (!src || !dest) {
  278. return 0;
  279. }
  280. uintptr_t ret = ngx_escape_uri((unsigned char *)dest, (unsigned char *)src, len, NGX_ESCAPE_URI_COMPONENT);
  281. return (uint32_t)(ret - (uintptr_t)dest);
  282. }
  283. void example_uri_decode(char *dest, const char *src, size_t len)
  284. {
  285. if (!src || !dest) {
  286. return;
  287. }
  288. unsigned char *src_ptr = (unsigned char *)src;
  289. unsigned char *dst_ptr = (unsigned char *)dest;
  290. ngx_unescape_uri(&dst_ptr, &src_ptr, len, NGX_UNESCAPE_URI);
  291. }