blob: 2b9c59a5e62d6c40660fed5c23aedb51933bb8b4 [file] [log] [blame]
Igor Sysoevd90282d2004-09-28 08:34:51 +00001
2/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00007#ifndef _NGX_STRING_H_INCLUDED_
8#define _NGX_STRING_H_INCLUDED_
9
10
11#include <ngx_config.h>
Igor Sysoev1c104622003-06-03 15:42:58 +000012#include <ngx_core.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000013
14
Igor Sysoeva0bb31f2002-12-02 16:09:40 +000015typedef struct {
Igor Sysoevf42ed052007-07-17 09:23:23 +000016 size_t len;
17 u_char *data;
Igor Sysoeva0bb31f2002-12-02 16:09:40 +000018} ngx_str_t;
19
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000020
Igor Sysoev3338cfd2006-05-11 14:43:47 +000021typedef struct {
Igor Sysoevf42ed052007-07-17 09:23:23 +000022 ngx_str_t key;
23 ngx_str_t value;
Igor Sysoev3338cfd2006-05-11 14:43:47 +000024} ngx_keyval_t;
25
26
Igor Sysoevf42ed052007-07-17 09:23:23 +000027typedef struct {
Igor Sysoev5f3521c2008-05-15 15:09:39 +000028 unsigned len:28;
Igor Sysoevf42ed052007-07-17 09:23:23 +000029
30 unsigned valid:1;
Igor Sysoev2d3f3f62007-10-14 18:56:15 +000031 unsigned no_cacheable:1;
Igor Sysoevf42ed052007-07-17 09:23:23 +000032 unsigned not_found:1;
Igor Sysoev5f3521c2008-05-15 15:09:39 +000033 unsigned escape:1;
Igor Sysoevf42ed052007-07-17 09:23:23 +000034
35 u_char *data;
36} ngx_variable_value_t;
37
38
Igor Sysoev722231f2007-02-14 18:51:19 +000039#define ngx_string(str) { sizeof(str) - 1, (u_char *) str }
40#define ngx_null_string { 0, NULL }
Igor Sysoev05b1a8f2010-05-14 09:56:37 +000041#define ngx_str_set(str, text) \
42 (str)->len = sizeof(text) - 1; (str)->data = (u_char *) text
43#define ngx_str_null(str) (str)->len = 0; (str)->data = NULL
Igor Sysoev960ffa42002-12-26 07:24:21 +000044
45
Igor Sysoev722231f2007-02-14 18:51:19 +000046#define ngx_tolower(c) (u_char) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
47#define ngx_toupper(c) (u_char) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000048
Igor Sysoev777b0192008-08-04 10:07:00 +000049void ngx_strlow(u_char *dst, u_char *src, size_t n);
50
Igor Sysoev3646a162004-03-14 20:46:25 +000051
Igor Sysoev1b735832004-11-11 14:07:14 +000052#define ngx_strncmp(s1, s2, n) strncmp((const char *) s1, (const char *) s2, n)
53
Igor Sysoev3646a162004-03-14 20:46:25 +000054
Igor Sysoev09c684b2005-11-09 17:25:55 +000055/* msvc and icc7 compile strcmp() to inline loop */
Igor Sysoevd90282d2004-09-28 08:34:51 +000056#define ngx_strcmp(s1, s2) strcmp((const char *) s1, (const char *) s2)
Igor Sysoev3646a162004-03-14 20:46:25 +000057
Igor Sysoev1b735832004-11-11 14:07:14 +000058
Igor Sysoevd90282d2004-09-28 08:34:51 +000059#define ngx_strstr(s1, s2) strstr((const char *) s1, (const char *) s2)
60#define ngx_strlen(s) strlen((const char *) s)
Igor Sysoev3646a162004-03-14 20:46:25 +000061
Igor Sysoevcaa4a452008-08-26 14:16:36 +000062#define ngx_strchr(s1, c) strchr((const char *) s1, (int) c)
63
64static ngx_inline u_char *
65ngx_strlchr(u_char *p, u_char *last, u_char c)
66{
67 while (p < last) {
68
69 if (*p == c) {
70 return p;
71 }
72
73 p++;
74 }
75
76 return NULL;
77}
78
Igor Sysoev1b735832004-11-11 14:07:14 +000079
Igor Sysoev6abfde62003-07-01 15:00:03 +000080/*
Igor Sysoev09c684b2005-11-09 17:25:55 +000081 * msvc and icc7 compile memset() to the inline "rep stos"
Igor Sysoevd90282d2004-09-28 08:34:51 +000082 * while ZeroMemory() and bzero() are the calls.
Igor Sysoev09c684b2005-11-09 17:25:55 +000083 * icc7 may also inline several mov's of a zeroed register for small blocks.
Igor Sysoev6abfde62003-07-01 15:00:03 +000084 */
Igor Sysoeve31e90b2005-05-19 13:25:22 +000085#define ngx_memzero(buf, n) (void) memset(buf, 0, n)
86#define ngx_memset(buf, c, n) (void) memset(buf, c, n)
Igor Sysoev6abfde62003-07-01 15:00:03 +000087
Igor Sysoev1b735832004-11-11 14:07:14 +000088
Igor Sysoevd3283ff2005-12-05 13:18:09 +000089#if (NGX_MEMCPY_LIMIT)
90
91void *ngx_memcpy(void *dst, void *src, size_t n);
Igor Sysoevdaab61e2010-06-24 15:17:06 +000092#define ngx_cpymem(dst, src, n) (((u_char *) ngx_memcpy(dst, src, n)) + (n))
Igor Sysoevd3283ff2005-12-05 13:18:09 +000093
94#else
95
Igor Sysoev09c684b2005-11-09 17:25:55 +000096/*
97 * gcc3, msvc, and icc7 compile memcpy() to the inline "rep movs".
98 * gcc3 compiles memcpy(d, s, 4) to the inline "mov"es.
99 * icc8 compile memcpy(d, s, 4) to the inline "mov"es or XMM moves.
100 */
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000101#define ngx_memcpy(dst, src, n) (void) memcpy(dst, src, n)
Igor Sysoevdaab61e2010-06-24 15:17:06 +0000102#define ngx_cpymem(dst, src, n) (((u_char *) memcpy(dst, src, n)) + (n))
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000103
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000104#endif
105
Igor Sysoev1b735832004-11-11 14:07:14 +0000106
Igor Sysoev09c684b2005-11-09 17:25:55 +0000107#if ( __INTEL_COMPILER >= 800 )
108
109/*
110 * the simple inline cycle copies the variable length strings up to 16
111 * bytes faster than icc8 autodetecting _intel_fast_memcpy()
112 */
113
114static ngx_inline u_char *
115ngx_copy(u_char *dst, u_char *src, size_t len)
116{
117 if (len < 17) {
118
119 while (len) {
120 *dst++ = *src++;
121 len--;
122 }
123
124 return dst;
125
126 } else {
127 return ngx_cpymem(dst, src, len);
128 }
129}
130
131#else
132
133#define ngx_copy ngx_cpymem
134
135#endif
136
137
Igor Sysoevd63104e2011-04-12 08:02:46 +0000138#define ngx_memmove(dst, src, n) (void) memmove(dst, src, n)
139#define ngx_movemem(dst, src, n) (((u_char *) memmove(dst, src, n)) + (n))
140
141
Igor Sysoev09c684b2005-11-09 17:25:55 +0000142/* msvc and icc7 compile memcmp() to the inline loop */
Igor Sysoev2e8f0d02007-11-23 17:00:11 +0000143#define ngx_memcmp(s1, s2, n) memcmp((const char *) s1, (const char *) s2, n)
Igor Sysoev6abfde62003-07-01 15:00:03 +0000144
Igor Sysoev1b735832004-11-11 14:07:14 +0000145
Igor Sysoev10a543a2004-03-16 07:10:12 +0000146u_char *ngx_cpystrn(u_char *dst, u_char *src, size_t n);
Igor Sysoev805d9db2005-02-03 19:33:37 +0000147u_char *ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src);
Igor Sysoev4d656dc2005-03-22 16:02:46 +0000148u_char * ngx_cdecl ngx_sprintf(u_char *buf, const char *fmt, ...);
149u_char * ngx_cdecl ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...);
Igor Sysoev4e1fe032009-04-27 12:51:33 +0000150u_char * ngx_cdecl ngx_slprintf(u_char *buf, u_char *last, const char *fmt,
151 ...);
152u_char *ngx_vslprintf(u_char *buf, u_char *last, const char *fmt, va_list args);
153#define ngx_vsnprintf(buf, max, fmt, args) \
154 ngx_vslprintf(buf, buf + (max), fmt, args)
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000155
Igor Sysoev722231f2007-02-14 18:51:19 +0000156ngx_int_t ngx_strcasecmp(u_char *s1, u_char *s2);
157ngx_int_t ngx_strncasecmp(u_char *s1, u_char *s2, size_t n);
158
Igor Sysoev35fe5fd2007-10-01 14:48:33 +0000159u_char *ngx_strnstr(u_char *s1, char *s2, size_t n);
160
Igor Sysoev1bd98702007-09-26 19:25:52 +0000161u_char *ngx_strstrn(u_char *s1, char *s2, size_t n);
162u_char *ngx_strcasestrn(u_char *s1, char *s2, size_t n);
Igor Sysoeva514d682009-04-04 17:31:54 +0000163u_char *ngx_strlcasestrn(u_char *s1, u_char *last, u_char *s2, size_t n);
Igor Sysoev1bd98702007-09-26 19:25:52 +0000164
Igor Sysoev10a543a2004-03-16 07:10:12 +0000165ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n);
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000166ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n);
Igor Sysoevec3cabd2007-01-12 21:58:02 +0000167ngx_int_t ngx_memn2cmp(u_char *s1, u_char *s2, size_t n1, size_t n2);
Igor Sysoev96e36ef2009-09-12 09:28:37 +0000168ngx_int_t ngx_dns_strcmp(u_char *s1, u_char *s2);
Igor Sysoev18684bd2004-05-20 17:33:52 +0000169
Igor Sysoev10a543a2004-03-16 07:10:12 +0000170ngx_int_t ngx_atoi(u_char *line, size_t n);
Igor Sysoevd2b687c2010-05-14 09:01:30 +0000171ngx_int_t ngx_atofp(u_char *line, size_t n, size_t point);
Igor Sysoevc1571722005-03-19 12:38:37 +0000172ssize_t ngx_atosz(u_char *line, size_t n);
173off_t ngx_atoof(u_char *line, size_t n);
174time_t ngx_atotm(u_char *line, size_t n);
Igor Sysoev18684bd2004-05-20 17:33:52 +0000175ngx_int_t ngx_hextoi(u_char *line, size_t n);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000176
Igor Sysoeva03fa362007-12-17 21:06:17 +0000177u_char *ngx_hex_dump(u_char *dst, u_char *src, size_t len);
Igor Sysoev9cc1ace2003-11-04 22:12:39 +0000178
Igor Sysoeva7c4a2a2004-08-29 03:55:41 +0000179
180#define ngx_base64_encoded_length(len) (((len + 2) / 3) * 4)
181#define ngx_base64_decoded_length(len) (((len + 3) / 4) * 3)
182
Igor Sysoev924bd792004-10-11 15:07:03 +0000183void ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src);
184ngx_int_t ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src);
Igor Sysoev94e9aaa2010-09-02 14:37:16 +0000185ngx_int_t ngx_decode_base64url(ngx_str_t *dst, ngx_str_t *src);
Igor Sysoev1b735832004-11-11 14:07:14 +0000186
Igor Sysoeva0898572008-07-29 14:41:34 +0000187uint32_t ngx_utf8_decode(u_char **p, size_t n);
188size_t ngx_utf8_length(u_char *p, size_t n);
189u_char *ngx_utf8_cpystrn(u_char *dst, u_char *src, size_t n, size_t len);
Igor Sysoev5192b362005-07-08 14:34:20 +0000190
Igor Sysoev1b735832004-11-11 14:07:14 +0000191
Maxim Dounin6226fe32011-10-11 17:56:51 +0000192#define NGX_ESCAPE_URI 0
193#define NGX_ESCAPE_ARGS 1
194#define NGX_ESCAPE_URI_COMPONENT 2
195#define NGX_ESCAPE_HTML 3
196#define NGX_ESCAPE_REFRESH 4
197#define NGX_ESCAPE_MEMCACHED 5
198#define NGX_ESCAPE_MAIL_AUTH 6
Igor Sysoevae33d012006-01-17 20:04:32 +0000199
Igor Sysoevf0a51cf2007-10-22 10:19:17 +0000200#define NGX_UNESCAPE_URI 1
201#define NGX_UNESCAPE_REDIRECT 2
Igor Sysoev1b735832004-11-11 14:07:14 +0000202
Igor Sysoev805d9db2005-02-03 19:33:37 +0000203uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size,
Igor Sysoevd039a2e2005-02-22 14:40:13 +0000204 ngx_uint_t type);
Igor Sysoevae33d012006-01-17 20:04:32 +0000205void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, ngx_uint_t type);
Igor Sysoev1730c752007-09-27 09:36:50 +0000206uintptr_t ngx_escape_html(u_char *dst, u_char *src, size_t size);
207
Igor Sysoev967fd632004-08-27 15:40:59 +0000208
Igor Sysoev0923d082010-06-23 15:31:33 +0000209typedef struct {
210 ngx_rbtree_node_t node;
211 ngx_str_t str;
212} ngx_str_node_t;
213
214
215void ngx_str_rbtree_insert_value(ngx_rbtree_node_t *temp,
216 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
217ngx_str_node_t *ngx_str_rbtree_lookup(ngx_rbtree_t *rbtree, ngx_str_t *name,
218 uint32_t hash);
219
Igor Sysoev9cc1ace2003-11-04 22:12:39 +0000220
Igor Sysoev35921282007-05-21 14:05:23 +0000221void ngx_sort(void *base, size_t n, size_t size,
Igor Sysoevde8ec1e2008-03-24 13:04:02 +0000222 ngx_int_t (*cmp)(const void *, const void *));
Igor Sysoev3f707822007-07-22 19:18:59 +0000223#define ngx_qsort qsort
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000224
Igor Sysoev13933252003-05-29 13:02:09 +0000225
Igor Sysoev3f707822007-07-22 19:18:59 +0000226#define ngx_value_helper(n) #n
227#define ngx_value(n) ngx_value_helper(n)
Igor Sysoeva9830112003-05-19 16:39:14 +0000228
229
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000230#endif /* _NGX_STRING_H_INCLUDED_ */